1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. /**
  59. * Bare-bones, unsafe, fast string buffer. No thread-safety, no
  60. * parameter range checking, exposed fields. Note that in typical
  61. * applications, thread-safety of a StringBuffer is a somewhat
  62. * dubious concept in any case.
  63. * <p>
  64. * Note that Stree and DTM used a single FastStringBuffer as a string pool,
  65. * by recording start and length indices within this single buffer. This
  66. * minimizes heap overhead, but of course requires more work when retrieving
  67. * the data.
  68. * <p>
  69. * FastStringBuffer operates as a "chunked buffer". Doing so
  70. * reduces the need to recopy existing information when an append
  71. * exceeds the space available; we just allocate another chunk and
  72. * flow across to it. (The array of chunks may need to grow,
  73. * admittedly, but that's a much smaller object.) Some excess
  74. * recopying may arise when we extract Strings which cross chunk
  75. * boundaries; larger chunks make that less frequent.
  76. * <p>
  77. * The size values are parameterized, to allow tuning this code. In
  78. * theory, Result Tree Fragments might want to be tuned differently
  79. * from the main document's text.
  80. * <p>
  81. * %REVIEW% An experiment in self-tuning is
  82. * included in the code (using nested FastStringBuffers to achieve
  83. * variation in chunk sizes), but this implementation has proven to
  84. * be problematic when data may be being copied from the FSB into itself.
  85. * We should either re-architect that to make this safe (if possible)
  86. * or remove that code and clean up for performance/maintainability reasons.
  87. * <p>
  88. */
  89. public class FastStringBuffer
  90. {
  91. // If nonzero, forces the inial chunk size.
  92. /**/static final int DEBUG_FORCE_INIT_BITS=0;
  93. // %BUG% %REVIEW% *****PROBLEM SUSPECTED: If data from an FSB is being copied
  94. // back into the same FSB (variable set from previous variable, for example)
  95. // and blocksize changes in mid-copy... there's risk of severe malfunction in
  96. // the read process, due to how the resizing code re-jiggers storage. Arggh.
  97. // If we want to retain the variable-size-block feature, we need to reconsider
  98. // that issue. For now, I have forced us into fixed-size mode.
  99. static boolean DEBUG_FORCE_FIXED_CHUNKSIZE=true;
  100. /** Manefest constant: Suppress leading whitespace.
  101. * This should be used when normalize-to-SAX is called for the first chunk of a
  102. * multi-chunk output, or one following unsuppressed whitespace in a previous
  103. * chunk.
  104. * @see sendNormalizedSAXcharacters(char[],int,int,org.xml.sax.ContentHandler,int)
  105. */
  106. public static final int SUPPRESS_LEADING_WS=0x01;
  107. /** Manefest constant: Suppress trailing whitespace.
  108. * This should be used when normalize-to-SAX is called for the last chunk of a
  109. * multi-chunk output; it may have to be or'ed with SUPPRESS_LEADING_WS.
  110. */
  111. public static final int SUPPRESS_TRAILING_WS=0x02;
  112. /** Manefest constant: Suppress both leading and trailing whitespace.
  113. * This should be used when normalize-to-SAX is called for a complete string.
  114. * (I'm not wild about the name of this one. Ideas welcome.)
  115. * @see sendNormalizedSAXcharacters(char[],int,int,org.xml.sax.ContentHandler,int)
  116. */
  117. public static final int SUPPRESS_BOTH
  118. = SUPPRESS_LEADING_WS | SUPPRESS_TRAILING_WS;
  119. /** Manefest constant: Carry trailing whitespace of one chunk as leading
  120. * whitespace of the next chunk. Used internally; I don't see any reason
  121. * to make it public right now.
  122. */
  123. private static final int CARRY_WS=0x04;
  124. /**
  125. * Field m_chunkBits sets our chunking strategy, by saying how many
  126. * bits of index can be used within a single chunk before flowing over
  127. * to the next chunk. For example, if m_chunkbits is set to 15, each
  128. * chunk can contain up to 2^15 (32K) characters
  129. */
  130. int m_chunkBits = 15;
  131. /**
  132. * Field m_maxChunkBits affects our chunk-growth strategy, by saying what
  133. * the largest permissible chunk size is in this particular FastStringBuffer
  134. * hierarchy.
  135. */
  136. int m_maxChunkBits = 15;
  137. /**
  138. * Field m_rechunkBits affects our chunk-growth strategy, by saying how
  139. * many chunks should be allocated at one size before we encapsulate them
  140. * into the first chunk of the next size up. For example, if m_rechunkBits
  141. * is set to 3, then after 8 chunks at a given size we will rebundle
  142. * them as the first element of a FastStringBuffer using a chunk size
  143. * 8 times larger (chunkBits shifted left three bits).
  144. */
  145. int m_rebundleBits = 2;
  146. /**
  147. * Field m_chunkSize establishes the maximum size of one chunk of the array
  148. * as 2**chunkbits characters.
  149. * (Which may also be the minimum size if we aren't tuning for storage)
  150. */
  151. int m_chunkSize; // =1<<(m_chunkBits-1);
  152. /**
  153. * Field m_chunkMask is m_chunkSize-1 -- in other words, m_chunkBits
  154. * worth of low-order '1' bits, useful for shift-and-mask addressing
  155. * within the chunks.
  156. */
  157. int m_chunkMask; // =m_chunkSize-1;
  158. /**
  159. * Field m_array holds the string buffer's text contents, using an
  160. * array-of-arrays. Note that this array, and the arrays it contains, may be
  161. * reallocated when necessary in order to allow the buffer to grow;
  162. * references to them should be considered to be invalidated after any
  163. * append. However, the only time these arrays are directly exposed
  164. * is in the sendSAXcharacters call.
  165. */
  166. char[][] m_array;
  167. /**
  168. * Field m_lastChunk is an index into m_array[], pointing to the last
  169. * chunk of the Chunked Array currently in use. Note that additional
  170. * chunks may actually be allocated, eg if the FastStringBuffer had
  171. * previously been truncated or if someone issued an ensureSpace request.
  172. * <p>
  173. * The insertion point for append operations is addressed by the combination
  174. * of m_lastChunk and m_firstFree.
  175. */
  176. int m_lastChunk = 0;
  177. /**
  178. * Field m_firstFree is an index into m_array[m_lastChunk][], pointing to
  179. * the first character in the Chunked Array which is not part of the
  180. * FastStringBuffer's current content. Since m_array[][] is zero-based,
  181. * the length of that content can be calculated as
  182. * (m_lastChunk<<m_chunkBits) + m_firstFree
  183. */
  184. int m_firstFree = 0;
  185. /**
  186. * Field m_innerFSB, when non-null, is a FastStringBuffer whose total
  187. * length equals m_chunkSize, and which replaces m_array[0]. This allows
  188. * building a hierarchy of FastStringBuffers, where early appends use
  189. * a smaller chunkSize (for less wasted memory overhead) but later
  190. * ones use a larger chunkSize (for less heap activity overhead).
  191. */
  192. FastStringBuffer m_innerFSB = null;
  193. /**
  194. * Construct a FastStringBuffer, with allocation policy as per parameters.
  195. * <p>
  196. * For coding convenience, I've expressed both allocation sizes in terms of
  197. * a number of bits. That's needed for the final size of a chunk,
  198. * to permit fast and efficient shift-and-mask addressing. It's less critical
  199. * for the inital size, and may be reconsidered.
  200. * <p>
  201. * An alternative would be to accept integer sizes and round to powers of two;
  202. * that really doesn't seem to buy us much, if anything.
  203. *
  204. * @param initChunkBits Length in characters of the initial allocation
  205. * of a chunk, expressed in log-base-2. (That is, 10 means allocate 1024
  206. * characters.) Later chunks will use larger allocation units, to trade off
  207. * allocation speed of large document against storage efficiency of small
  208. * ones.
  209. * @param maxChunkBits Number of character-offset bits that should be used for
  210. * addressing within a chunk. Maximum length of a chunk is 2^chunkBits
  211. * characters.
  212. * @param rebundleBits Number of character-offset bits that addressing should
  213. * advance before we attempt to take a step from initChunkBits to maxChunkBits
  214. */
  215. public FastStringBuffer(int initChunkBits, int maxChunkBits,
  216. int rebundleBits)
  217. {
  218. if(DEBUG_FORCE_INIT_BITS!=0) initChunkBits=DEBUG_FORCE_INIT_BITS;
  219. // %REVIEW%
  220. // Should this force to larger value, or smaller? Smaller less efficient, but if
  221. // someone requested variable mode it's because they care about storage space.
  222. // On the other hand, given the other changes I'm making, odds are that we should
  223. // adopt the larger size. Dither, dither, dither... This is just stopgap workaround
  224. // anyway; we need a permanant solution.
  225. //
  226. if(DEBUG_FORCE_FIXED_CHUNKSIZE) maxChunkBits=initChunkBits;
  227. //if(DEBUG_FORCE_FIXED_CHUNKSIZE) initChunkBits=maxChunkBits;
  228. m_array = new char[16][];
  229. // Don't bite off more than we're prepared to swallow!
  230. if (initChunkBits > maxChunkBits)
  231. initChunkBits = maxChunkBits;
  232. m_chunkBits = initChunkBits;
  233. m_maxChunkBits = maxChunkBits;
  234. m_rebundleBits = rebundleBits;
  235. m_chunkSize = 1 << (initChunkBits);
  236. m_chunkMask = m_chunkSize - 1;
  237. m_array[0] = new char[m_chunkSize];
  238. }
  239. /**
  240. * Construct a FastStringBuffer, using a default rebundleBits value.
  241. *
  242. * NEEDSDOC @param initChunkBits
  243. * NEEDSDOC @param maxChunkBits
  244. */
  245. public FastStringBuffer(int initChunkBits, int maxChunkBits)
  246. {
  247. this(initChunkBits, maxChunkBits, 2);
  248. }
  249. /**
  250. * Construct a FastStringBuffer, using default maxChunkBits and
  251. * rebundleBits values.
  252. * <p>
  253. * ISSUE: Should this call assert initial size, or fixed size?
  254. * Now configured as initial, with a default for fixed.
  255. *
  256. * @param
  257. *
  258. * NEEDSDOC @param initChunkBits
  259. */
  260. public FastStringBuffer(int initChunkBits)
  261. {
  262. this(initChunkBits, 15, 2);
  263. }
  264. /**
  265. * Construct a FastStringBuffer, using a default allocation policy.
  266. */
  267. public FastStringBuffer()
  268. {
  269. // 10 bits is 1K. 15 bits is 32K. Remember that these are character
  270. // counts, so actual memory allocation unit is doubled for UTF-16 chars.
  271. //
  272. // For reference: In the original FastStringBuffer, we simply
  273. // overallocated by blocksize (default 1KB) on each buffer-growth.
  274. this(10, 15, 2);
  275. }
  276. /**
  277. * Get the length of the list. Synonym for length().
  278. *
  279. * @return the number of characters in the FastStringBuffer's content.
  280. */
  281. public final int size()
  282. {
  283. return (m_lastChunk << m_chunkBits) + m_firstFree;
  284. }
  285. /**
  286. * Get the length of the list. Synonym for size().
  287. *
  288. * @return the number of characters in the FastStringBuffer's content.
  289. */
  290. public final int length()
  291. {
  292. return (m_lastChunk << m_chunkBits) + m_firstFree;
  293. }
  294. /**
  295. * Discard the content of the FastStringBuffer, and most of the memory
  296. * that was allocated by it, restoring the initial state. Note that this
  297. * may eventually be different from setLength(0), which see.
  298. */
  299. public final void reset()
  300. {
  301. m_lastChunk = 0;
  302. m_firstFree = 0;
  303. // Recover the original chunk size
  304. FastStringBuffer innermost = this;
  305. while (innermost.m_innerFSB != null)
  306. {
  307. innermost = innermost.m_innerFSB;
  308. }
  309. m_chunkBits = innermost.m_chunkBits;
  310. m_chunkSize = innermost.m_chunkSize;
  311. m_chunkMask = innermost.m_chunkMask;
  312. // Discard the hierarchy
  313. m_innerFSB = null;
  314. m_array = new char[16][0];
  315. m_array[0] = new char[m_chunkSize];
  316. }
  317. /**
  318. * Directly set how much of the FastStringBuffer's storage is to be
  319. * considered part of its content. This is a fast but hazardous
  320. * operation. It is not protected against negative values, or values
  321. * greater than the amount of storage currently available... and even
  322. * if additional storage does exist, its contents are unpredictable.
  323. * The only safe use for our setLength() is to truncate the FastStringBuffer
  324. * to a shorter string.
  325. *
  326. * @param l New length. If l<0 or l>=getLength(), this operation will
  327. * not report an error but future operations will almost certainly fail.
  328. */
  329. public final void setLength(int l)
  330. {
  331. m_lastChunk = l >>> m_chunkBits;
  332. if (m_lastChunk == 0 && m_innerFSB != null)
  333. {
  334. // Replace this FSB with the appropriate inner FSB, truncated
  335. m_innerFSB.setLength(l, this);
  336. }
  337. else
  338. {
  339. m_firstFree = l & m_chunkMask;
  340. // There's an edge case if l is an exact multiple of m_chunkBits, which risks leaving
  341. // us pointing at the start of a chunk which has not yet been allocated. Rather than
  342. // pay the cost of dealing with that in the append loops (more scattered and more
  343. // inner-loop), we correct it here by moving to the safe side of that
  344. // line -- as we would have left the indexes had we appended up to that point.
  345. if(m_firstFree==0 && m_lastChunk>0)
  346. {
  347. --m_lastChunk;
  348. m_firstFree=m_chunkSize;
  349. }
  350. }
  351. }
  352. /**
  353. * Subroutine for the public setLength() method. Deals with the fact
  354. * that truncation may require restoring one of the innerFSBs
  355. *
  356. * NEEDSDOC @param l
  357. * NEEDSDOC @param rootFSB
  358. */
  359. private final void setLength(int l, FastStringBuffer rootFSB)
  360. {
  361. m_lastChunk = l >>> m_chunkBits;
  362. if (m_lastChunk == 0 && m_innerFSB != null)
  363. {
  364. m_innerFSB.setLength(l, rootFSB);
  365. }
  366. else
  367. {
  368. // Undo encapsulation -- pop the innerFSB data back up to root.
  369. // Inefficient, but attempts to keep the code simple.
  370. rootFSB.m_chunkBits = m_chunkBits;
  371. rootFSB.m_maxChunkBits = m_maxChunkBits;
  372. rootFSB.m_rebundleBits = m_rebundleBits;
  373. rootFSB.m_chunkSize = m_chunkSize;
  374. rootFSB.m_chunkMask = m_chunkMask;
  375. rootFSB.m_array = m_array;
  376. rootFSB.m_innerFSB = m_innerFSB;
  377. rootFSB.m_lastChunk = m_lastChunk;
  378. // Finally, truncate this sucker.
  379. rootFSB.m_firstFree = l & m_chunkMask;
  380. }
  381. }
  382. /**
  383. * Note that this operation has been somewhat deoptimized by the shift to a
  384. * chunked array, as there is no factory method to produce a String object
  385. * directly from an array of arrays and hence a double copy is needed.
  386. * By using ensureCapacity we hope to minimize the heap overhead of building
  387. * the intermediate StringBuffer.
  388. * <p>
  389. * (It really is a pity that Java didn't design String as a final subclass
  390. * of MutableString, rather than having StringBuffer be a separate hierarchy.
  391. * We'd avoid a <strong>lot</strong> of double-buffering.)
  392. *
  393. * @return the contents of the FastStringBuffer as a standard Java string.
  394. */
  395. public final String toString()
  396. {
  397. int length = (m_lastChunk << m_chunkBits) + m_firstFree;
  398. return getString(new StringBuffer(length), 0, 0, length).toString();
  399. }
  400. /**
  401. * Append a single character onto the FastStringBuffer, growing the
  402. * storage if necessary.
  403. * <p>
  404. * NOTE THAT after calling append(), previously obtained
  405. * references to m_array[][] may no longer be valid....
  406. * though in fact they should be in this instance.
  407. *
  408. * @param value character to be appended.
  409. */
  410. public final void append(char value)
  411. {
  412. char[] chunk;
  413. // We may have preallocated chunks. If so, all but last should
  414. // be at full size.
  415. boolean lastchunk = (m_lastChunk + 1 == m_array.length);
  416. if (m_firstFree < m_chunkSize) // Simplified test single-character-fits
  417. chunk = m_array[m_lastChunk];
  418. else
  419. {
  420. // Extend array?
  421. int i = m_array.length;
  422. if (m_lastChunk + 1 == i)
  423. {
  424. char[][] newarray = new char[i + 16][];
  425. System.arraycopy(m_array, 0, newarray, 0, i);
  426. m_array = newarray;
  427. }
  428. // Advance one chunk
  429. chunk = m_array[++m_lastChunk];
  430. if (chunk == null)
  431. {
  432. // Hierarchical encapsulation
  433. if (m_lastChunk == 1 << m_rebundleBits
  434. && m_chunkBits < m_maxChunkBits)
  435. {
  436. // Should do all the work of both encapsulating
  437. // existing data and establishing new sizes/offsets
  438. m_innerFSB = new FastStringBuffer(this);
  439. }
  440. // Add a chunk.
  441. chunk = m_array[m_lastChunk] = new char[m_chunkSize];
  442. }
  443. m_firstFree = 0;
  444. }
  445. // Space exists in the chunk. Append the character.
  446. chunk[m_firstFree++] = value;
  447. }
  448. /**
  449. * Append the contents of a String onto the FastStringBuffer,
  450. * growing the storage if necessary.
  451. * <p>
  452. * NOTE THAT after calling append(), previously obtained
  453. * references to m_array[] may no longer be valid.
  454. *
  455. * @param value String whose contents are to be appended.
  456. */
  457. public final void append(String value)
  458. {
  459. if (value == null)
  460. return;
  461. int strlen = value.length();
  462. if (0 == strlen)
  463. return;
  464. int copyfrom = 0;
  465. char[] chunk = m_array[m_lastChunk];
  466. int available = m_chunkSize - m_firstFree;
  467. // Repeat while data remains to be copied
  468. while (strlen > 0)
  469. {
  470. // Copy what fits
  471. if (available > strlen)
  472. available = strlen;
  473. value.getChars(copyfrom, copyfrom + available, m_array[m_lastChunk],
  474. m_firstFree);
  475. strlen -= available;
  476. copyfrom += available;
  477. // If there's more left, allocate another chunk and continue
  478. if (strlen > 0)
  479. {
  480. // Extend array?
  481. int i = m_array.length;
  482. if (m_lastChunk + 1 == i)
  483. {
  484. char[][] newarray = new char[i + 16][];
  485. System.arraycopy(m_array, 0, newarray, 0, i);
  486. m_array = newarray;
  487. }
  488. // Advance one chunk
  489. chunk = m_array[++m_lastChunk];
  490. if (chunk == null)
  491. {
  492. // Hierarchical encapsulation
  493. if (m_lastChunk == 1 << m_rebundleBits
  494. && m_chunkBits < m_maxChunkBits)
  495. {
  496. // Should do all the work of both encapsulating
  497. // existing data and establishing new sizes/offsets
  498. m_innerFSB = new FastStringBuffer(this);
  499. }
  500. // Add a chunk.
  501. chunk = m_array[m_lastChunk] = new char[m_chunkSize];
  502. }
  503. available = m_chunkSize;
  504. m_firstFree = 0;
  505. }
  506. }
  507. // Adjust the insert point in the last chunk, when we've reached it.
  508. m_firstFree += available;
  509. }
  510. /**
  511. * Append the contents of a StringBuffer onto the FastStringBuffer,
  512. * growing the storage if necessary.
  513. * <p>
  514. * NOTE THAT after calling append(), previously obtained
  515. * references to m_array[] may no longer be valid.
  516. *
  517. * @param value StringBuffer whose contents are to be appended.
  518. */
  519. public final void append(StringBuffer value)
  520. {
  521. if (value == null)
  522. return;
  523. int strlen = value.length();
  524. if (0 == strlen)
  525. return;
  526. int copyfrom = 0;
  527. char[] chunk = m_array[m_lastChunk];
  528. int available = m_chunkSize - m_firstFree;
  529. // Repeat while data remains to be copied
  530. while (strlen > 0)
  531. {
  532. // Copy what fits
  533. if (available > strlen)
  534. available = strlen;
  535. value.getChars(copyfrom, copyfrom + available, m_array[m_lastChunk],
  536. m_firstFree);
  537. strlen -= available;
  538. copyfrom += available;
  539. // If there's more left, allocate another chunk and continue
  540. if (strlen > 0)
  541. {
  542. // Extend array?
  543. int i = m_array.length;
  544. if (m_lastChunk + 1 == i)
  545. {
  546. char[][] newarray = new char[i + 16][];
  547. System.arraycopy(m_array, 0, newarray, 0, i);
  548. m_array = newarray;
  549. }
  550. // Advance one chunk
  551. chunk = m_array[++m_lastChunk];
  552. if (chunk == null)
  553. {
  554. // Hierarchical encapsulation
  555. if (m_lastChunk == 1 << m_rebundleBits
  556. && m_chunkBits < m_maxChunkBits)
  557. {
  558. // Should do all the work of both encapsulating
  559. // existing data and establishing new sizes/offsets
  560. m_innerFSB = new FastStringBuffer(this);
  561. }
  562. // Add a chunk.
  563. chunk = m_array[m_lastChunk] = new char[m_chunkSize];
  564. }
  565. available = m_chunkSize;
  566. m_firstFree = 0;
  567. }
  568. }
  569. // Adjust the insert point in the last chunk, when we've reached it.
  570. m_firstFree += available;
  571. }
  572. /**
  573. * Append part of the contents of a Character Array onto the
  574. * FastStringBuffer, growing the storage if necessary.
  575. * <p>
  576. * NOTE THAT after calling append(), previously obtained
  577. * references to m_array[] may no longer be valid.
  578. *
  579. * @param chars character array from which data is to be copied
  580. * @param start offset in chars of first character to be copied,
  581. * zero-based.
  582. * @param length number of characters to be copied
  583. */
  584. public final void append(char[] chars, int start, int length)
  585. {
  586. int strlen = length;
  587. if (0 == strlen)
  588. return;
  589. int copyfrom = start;
  590. char[] chunk = m_array[m_lastChunk];
  591. int available = m_chunkSize - m_firstFree;
  592. // Repeat while data remains to be copied
  593. while (strlen > 0)
  594. {
  595. // Copy what fits
  596. if (available > strlen)
  597. available = strlen;
  598. System.arraycopy(chars, copyfrom, m_array[m_lastChunk], m_firstFree,
  599. available);
  600. strlen -= available;
  601. copyfrom += available;
  602. // If there's more left, allocate another chunk and continue
  603. if (strlen > 0)
  604. {
  605. // Extend array?
  606. int i = m_array.length;
  607. if (m_lastChunk + 1 == i)
  608. {
  609. char[][] newarray = new char[i + 16][];
  610. System.arraycopy(m_array, 0, newarray, 0, i);
  611. m_array = newarray;
  612. }
  613. // Advance one chunk
  614. chunk = m_array[++m_lastChunk];
  615. if (chunk == null)
  616. {
  617. // Hierarchical encapsulation
  618. if (m_lastChunk == 1 << m_rebundleBits
  619. && m_chunkBits < m_maxChunkBits)
  620. {
  621. // Should do all the work of both encapsulating
  622. // existing data and establishing new sizes/offsets
  623. m_innerFSB = new FastStringBuffer(this);
  624. }
  625. // Add a chunk.
  626. chunk = m_array[m_lastChunk] = new char[m_chunkSize];
  627. }
  628. available = m_chunkSize;
  629. m_firstFree = 0;
  630. }
  631. }
  632. // Adjust the insert point in the last chunk, when we've reached it.
  633. m_firstFree += available;
  634. }
  635. /**
  636. * Append the contents of another FastStringBuffer onto
  637. * this FastStringBuffer, growing the storage if necessary.
  638. * <p>
  639. * NOTE THAT after calling append(), previously obtained
  640. * references to m_array[] may no longer be valid.
  641. *
  642. * @param value FastStringBuffer whose contents are
  643. * to be appended.
  644. */
  645. public final void append(FastStringBuffer value)
  646. {
  647. // Complicating factor here is that the two buffers may use
  648. // different chunk sizes, and even if they're the same we're
  649. // probably on a different alignment due to previously appended
  650. // data. We have to work through the source in bite-sized chunks.
  651. if (value == null)
  652. return;
  653. int strlen = value.length();
  654. if (0 == strlen)
  655. return;
  656. int copyfrom = 0;
  657. char[] chunk = m_array[m_lastChunk];
  658. int available = m_chunkSize - m_firstFree;
  659. // Repeat while data remains to be copied
  660. while (strlen > 0)
  661. {
  662. // Copy what fits
  663. if (available > strlen)
  664. available = strlen;
  665. int sourcechunk = (copyfrom + value.m_chunkSize - 1)
  666. >>> value.m_chunkBits;
  667. int sourcecolumn = copyfrom & value.m_chunkMask;
  668. int runlength = value.m_chunkSize - sourcecolumn;
  669. if (runlength > available)
  670. runlength = available;
  671. System.arraycopy(value.m_array[sourcechunk], sourcecolumn,
  672. m_array[m_lastChunk], m_firstFree, runlength);
  673. if (runlength != available)
  674. System.arraycopy(value.m_array[sourcechunk + 1], 0,
  675. m_array[m_lastChunk], m_firstFree + runlength,
  676. available - runlength);
  677. strlen -= available;
  678. copyfrom += available;
  679. // If there's more left, allocate another chunk and continue
  680. if (strlen > 0)
  681. {
  682. // Extend array?
  683. int i = m_array.length;
  684. if (m_lastChunk + 1 == i)
  685. {
  686. char[][] newarray = new char[i + 16][];
  687. System.arraycopy(m_array, 0, newarray, 0, i);
  688. m_array = newarray;
  689. }
  690. // Advance one chunk
  691. chunk = m_array[++m_lastChunk];
  692. if (chunk == null)
  693. {
  694. // Hierarchical encapsulation
  695. if (m_lastChunk == 1 << m_rebundleBits
  696. && m_chunkBits < m_maxChunkBits)
  697. {
  698. // Should do all the work of both encapsulating
  699. // existing data and establishing new sizes/offsets
  700. m_innerFSB = new FastStringBuffer(this);
  701. }
  702. // Add a chunk.
  703. chunk = m_array[m_lastChunk] = new char[m_chunkSize];
  704. }
  705. available = m_chunkSize;
  706. m_firstFree = 0;
  707. }
  708. }
  709. // Adjust the insert point in the last chunk, when we've reached it.
  710. m_firstFree += available;
  711. }
  712. /**
  713. * @return true if the specified range of characters are all whitespace,
  714. * as defined by XMLCharacterRecognizer.
  715. * <p>
  716. * CURRENTLY DOES NOT CHECK FOR OUT-OF-RANGE.
  717. *
  718. * @param start Offset of first character in the range.
  719. * @param length Number of characters to send.
  720. */
  721. public boolean isWhitespace(int start, int length)
  722. {
  723. int sourcechunk = start >>> m_chunkBits;
  724. int sourcecolumn = start & m_chunkMask;
  725. int available = m_chunkSize - sourcecolumn;
  726. boolean chunkOK;
  727. while (length > 0)
  728. {
  729. int runlength = (length <= available) ? length : available;
  730. if (sourcechunk == 0 && m_innerFSB != null)
  731. chunkOK = m_innerFSB.isWhitespace(sourcecolumn, runlength);
  732. else
  733. chunkOK = org.apache.xml.utils.XMLCharacterRecognizer.isWhiteSpace(
  734. m_array[sourcechunk], sourcecolumn, runlength);
  735. if (!chunkOK)
  736. return false;
  737. length -= runlength;
  738. ++sourcechunk;
  739. sourcecolumn = 0;
  740. available = m_chunkSize;
  741. }
  742. return true;
  743. }
  744. /**
  745. * @param start Offset of first character in the range.
  746. * @param length Number of characters to send.
  747. * @return a new String object initialized from the specified range of
  748. * characters.
  749. */
  750. public String getString(int start, int length)
  751. {
  752. return getString(new StringBuffer(length), start >>> m_chunkBits,
  753. start & m_chunkMask, length).toString();
  754. }
  755. /**
  756. * @param sb StringBuffer to be appended to
  757. * @param start Offset of first character in the range.
  758. * @param length Number of characters to send.
  759. * @return sb with the requested text appended to it
  760. */
  761. StringBuffer getString(StringBuffer sb, int start, int length)
  762. {
  763. return getString(sb, start >>> m_chunkBits, start & m_chunkMask, length);
  764. }
  765. /**
  766. * Internal support for toString() and getString().
  767. * PLEASE NOTE SIGNATURE CHANGE from earlier versions; it now appends into
  768. * and returns a StringBuffer supplied by the caller. This simplifies
  769. * m_innerFSB support.
  770. * <p>
  771. * Note that this operation has been somewhat deoptimized by the shift to a
  772. * chunked array, as there is no factory method to produce a String object
  773. * directly from an array of arrays and hence a double copy is needed.
  774. * By presetting length we hope to minimize the heap overhead of building
  775. * the intermediate StringBuffer.
  776. * <p>
  777. * (It really is a pity that Java didn't design String as a final subclass
  778. * of MutableString, rather than having StringBuffer be a separate hierarchy.
  779. * We'd avoid a <strong>lot</strong> of double-buffering.)
  780. *
  781. *
  782. * @param sb
  783. * @param startChunk
  784. * @param startColumn
  785. * @param length
  786. *
  787. * @return the contents of the FastStringBuffer as a standard Java string.
  788. */
  789. StringBuffer getString(StringBuffer sb, int startChunk, int startColumn,
  790. int length)
  791. {
  792. int stop = (startChunk << m_chunkBits) + startColumn + length;
  793. int stopChunk = stop >>> m_chunkBits;
  794. int stopColumn = stop & m_chunkMask;
  795. // Factored out
  796. //StringBuffer sb=new StringBuffer(length);
  797. for (int i = startChunk; i < stopChunk; ++i)
  798. {
  799. if (i == 0 && m_innerFSB != null)
  800. m_innerFSB.getString(sb, startColumn, m_chunkSize - startColumn);
  801. else
  802. sb.append(m_array[i], startColumn, m_chunkSize - startColumn);
  803. startColumn = 0; // after first chunk
  804. }
  805. if (stopChunk == 0 && m_innerFSB != null)
  806. m_innerFSB.getString(sb, startColumn, stopColumn - startColumn);
  807. else if (stopColumn > startColumn)
  808. sb.append(m_array[stopChunk], startColumn, stopColumn - startColumn);
  809. return sb;
  810. }
  811. /**
  812. * Get a single character from the string buffer.
  813. *
  814. *
  815. * @param pos character position requested.
  816. * @return A character from the requested position.
  817. */
  818. public char charAt(int pos)
  819. {
  820. int startChunk = pos >>> m_chunkBits;
  821. if (startChunk == 0 && m_innerFSB != null)
  822. return m_innerFSB.charAt(pos & m_chunkMask);
  823. else
  824. return m_array[startChunk][pos & m_chunkMask];
  825. }
  826. /**
  827. * Sends the specified range of characters as one or more SAX characters()
  828. * events.
  829. * Note that the buffer reference passed to the ContentHandler may be
  830. * invalidated if the FastStringBuffer is edited; it's the user's
  831. * responsibility to manage access to the FastStringBuffer to prevent this
  832. * problem from arising.
  833. * <p>
  834. * Note too that there is no promise that the output will be sent as a
  835. * single call. As is always true in SAX, one logical string may be split
  836. * across multiple blocks of memory and hence delivered as several
  837. * successive events.
  838. *
  839. * @param ch SAX ContentHandler object to receive the event.
  840. * @param start Offset of first character in the range.
  841. * @param length Number of characters to send.
  842. * @exception org.xml.sax.SAXException may be thrown by handler's
  843. * characters() method.
  844. */
  845. public void sendSAXcharacters(
  846. org.xml.sax.ContentHandler ch, int start, int length)
  847. throws org.xml.sax.SAXException
  848. {
  849. int stop = start + length;
  850. int startChunk = start >>> m_chunkBits;
  851. int startColumn = start & m_chunkMask;
  852. int stopChunk = stop >>> m_chunkBits;
  853. int stopColumn = stop & m_chunkMask;
  854. for (int i = startChunk; i < stopChunk; ++i)
  855. {
  856. if (i == 0 && m_innerFSB != null)
  857. m_innerFSB.sendSAXcharacters(ch, startColumn,
  858. m_chunkSize - startColumn);
  859. else
  860. ch.characters(m_array[i], startColumn, m_chunkSize - startColumn);
  861. startColumn = 0; // after first chunk
  862. }
  863. // Last, or only, chunk
  864. if (stopChunk == 0 && m_innerFSB != null)
  865. m_innerFSB.sendSAXcharacters(ch, startColumn, stopColumn - startColumn);
  866. else if (stopColumn > startColumn)
  867. {
  868. ch.characters(m_array[stopChunk], startColumn,
  869. stopColumn - startColumn);
  870. }
  871. }
  872. /**
  873. * Sends the specified range of characters as one or more SAX characters()
  874. * events, normalizing the characters according to XSLT rules.
  875. *
  876. * @param ch SAX ContentHandler object to receive the event.
  877. * @param start Offset of first character in the range.
  878. * @param length Number of characters to send.
  879. * @return normalization status to apply to next chunk (because we may
  880. * have been called recursively to process an inner FSB):
  881. * <dl>
  882. * <dt>0</dt>
  883. * <dd>if this output did not end in retained whitespace, and thus whitespace
  884. * at the start of the following chunk (if any) should be converted to a
  885. * single space.
  886. * <dt>SUPPRESS_LEADING_WS</dt>
  887. * <dd>if this output ended in retained whitespace, and thus whitespace
  888. * at the start of the following chunk (if any) should be completely
  889. * suppressed.</dd>
  890. * </dd>
  891. * </dl>
  892. * @exception org.xml.sax.SAXException may be thrown by handler's
  893. * characters() method.
  894. */
  895. public int sendNormalizedSAXcharacters(
  896. org.xml.sax.ContentHandler ch, int start, int length)
  897. throws org.xml.sax.SAXException
  898. {
  899. // This call always starts at the beginning of the
  900. // string being written out, either because it was called directly or
  901. // because it was an m_innerFSB recursion. This is important since
  902. // it gives us a well-known initial state for this flag:
  903. int stateForNextChunk=SUPPRESS_LEADING_WS;
  904. int stop = start + length;
  905. int startChunk = start >>> m_chunkBits;
  906. int startColumn = start & m_chunkMask;
  907. int stopChunk = stop >>> m_chunkBits;
  908. int stopColumn = stop & m_chunkMask;
  909. for (int i = startChunk; i < stopChunk; ++i)
  910. {
  911. if (i == 0 && m_innerFSB != null)
  912. stateForNextChunk=
  913. m_innerFSB.sendNormalizedSAXcharacters(ch, startColumn,
  914. m_chunkSize - startColumn);
  915. else
  916. stateForNextChunk=
  917. sendNormalizedSAXcharacters(m_array[i], startColumn,
  918. m_chunkSize - startColumn,
  919. ch,stateForNextChunk);
  920. startColumn = 0; // after first chunk
  921. }
  922. // Last, or only, chunk
  923. if (stopChunk == 0 && m_innerFSB != null)
  924. stateForNextChunk= // %REVIEW% Is this update really needed?
  925. m_innerFSB.sendNormalizedSAXcharacters(ch, startColumn, stopColumn - startColumn);
  926. else if (stopColumn > startColumn)
  927. {
  928. stateForNextChunk= // %REVIEW% Is this update really needed?
  929. sendNormalizedSAXcharacters(m_array[stopChunk],
  930. startColumn, stopColumn - startColumn,
  931. ch, stateForNextChunk | SUPPRESS_TRAILING_WS);
  932. }
  933. return stateForNextChunk;
  934. }
  935. static final char[] SINGLE_SPACE = {' '};
  936. /**
  937. * Internal method to directly normalize and dispatch the character array.
  938. * This version is aware of the fact that it may be called several times
  939. * in succession if the data is made up of multiple "chunks", and thus
  940. * must actively manage the handling of leading and trailing whitespace.
  941. *
  942. * Note: The recursion is due to the possible recursion of inner FSBs.
  943. *
  944. * @param ch The characters from the XML document.
  945. * @param start The start position in the array.
  946. * @param length The number of characters to read from the array.
  947. * @param handler SAX ContentHandler object to receive the event.
  948. * @param edgeTreatmentFlags How leading/trailing spaces should be handled.
  949. * This is a bitfield contining two flags, bitwise-ORed together:
  950. * <dl>
  951. * <dt>SUPPRESS_LEADING_WS</dt>
  952. * <dd>When false, causes leading whitespace to be converted to a single
  953. * space; when true, causes it to be discarded entirely.
  954. * Should be set TRUE for the first chunk, and (in multi-chunk output)
  955. * whenever the previous chunk ended in retained whitespace.</dd>
  956. * <dt>SUPPRESS_TRAILING_WS</dt>
  957. * <dd>When false, causes trailing whitespace to be converted to a single
  958. * space; when true, causes it to be discarded entirely.
  959. * Should be set TRUE for the last or only chunk.
  960. * </dd>
  961. * </dl>
  962. * @return normalization status, as in the edgeTreatmentFlags parameter:
  963. * <dl>
  964. * <dt>0</dt>
  965. * <dd>if this output did not end in retained whitespace, and thus whitespace
  966. * at the start of the following chunk (if any) should be converted to a
  967. * single space.
  968. * <dt>SUPPRESS_LEADING_WS</dt>
  969. * <dd>if this output ended in retained whitespace, and thus whitespace
  970. * at the start of the following chunk (if any) should be completely
  971. * suppressed.</dd>
  972. * </dd>
  973. * </dl>
  974. *
  975. *
  976. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  977. * wrapping another exception.
  978. */
  979. static int sendNormalizedSAXcharacters(char ch[],
  980. int start, int length,
  981. org.xml.sax.ContentHandler handler,
  982. int edgeTreatmentFlags)
  983. throws org.xml.sax.SAXException
  984. {
  985. int end = length + start;
  986. int scanpos=start;
  987. // Leading whitespaces should be _completely_ suppressed if and only if
  988. // (a) we're the first chunk in the normalized sequence or (b) the
  989. // previous chunk ended in a normalized-but-not-suppressed whitespace.
  990. // If no suppression required and first char is whitespace, than
  991. // add single space and suppress following white spaces
  992. if (XMLCharacterRecognizer.isWhiteSpace(ch[scanpos]))
  993. {
  994. if(0 == (edgeTreatmentFlags&SUPPRESS_LEADING_WS) )
  995. handler.characters(SINGLE_SPACE, 0, 1);
  996. while(++scanpos < end)
  997. {
  998. char c = ch[scanpos];
  999. if(!XMLCharacterRecognizer.isWhiteSpace(c))
  1000. break;
  1001. }
  1002. }
  1003. // %REVIEW% Do we really need both flags?
  1004. boolean whiteSpaceFound = false; // Last char seen was whitespace
  1005. // Pending whitespace. May be carried from previous chunk
  1006. boolean needToFlushSpace = 0!=(edgeTreatmentFlags&CARRY_WS);
  1007. int datapos = scanpos; // Start of non-whitespace data (if any)
  1008. for (; scanpos < end; scanpos++)
  1009. {
  1010. char c = ch[scanpos];
  1011. if (XMLCharacterRecognizer.isWhiteSpace(c))
  1012. {
  1013. if (!whiteSpaceFound)
  1014. {
  1015. whiteSpaceFound = true;
  1016. int len = (scanpos-datapos);
  1017. if( len > 0)
  1018. {
  1019. if(needToFlushSpace)
  1020. handler.characters(SINGLE_SPACE, 0, 1);
  1021. handler.characters(ch, datapos, len);
  1022. needToFlushSpace = true;
  1023. }
  1024. datapos = scanpos+1;
  1025. }
  1026. else
  1027. {
  1028. int nonwhitescan = scanpos+1; // Hunt for first nonwhite character after whitespace
  1029. for (; nonwhitescan < end; nonwhitescan++)
  1030. {
  1031. c = ch[nonwhitescan];
  1032. if(!XMLCharacterRecognizer.isWhiteSpace(c))
  1033. break;
  1034. }
  1035. if(nonwhitescan == end)
  1036. {
  1037. end = scanpos;
  1038. break; // Let the flush at the end handle it.
  1039. }
  1040. int len = (scanpos-datapos);
  1041. if(len > 0)
  1042. {
  1043. if(needToFlushSpace)
  1044. {
  1045. handler.characters(SINGLE_SPACE, 0, 1);
  1046. needToFlushSpace = false;
  1047. }
  1048. handler.characters(ch, datapos, len);
  1049. }
  1050. whiteSpaceFound = false;
  1051. datapos = scanpos = nonwhitescan;
  1052. }
  1053. }
  1054. else
  1055. {
  1056. whiteSpaceFound = false;
  1057. }
  1058. }
  1059. if (whiteSpaceFound)
  1060. scanpos--;
  1061. int len = (scanpos-datapos);
  1062. // If have non-space text, output it (possibly with a space before it)
  1063. // and
  1064. if(len > 0)
  1065. {
  1066. if(needToFlushSpace) // Pending space
  1067. handler.characters(SINGLE_SPACE, 0, 1); // Output single space
  1068. handler.characters(ch, datapos, len);
  1069. edgeTreatmentFlags &= ~(SUPPRESS_LEADING_WS | CARRY_WS);
  1070. }
  1071. // If we ended in (nonsuppressed) whitespace, tell the next chunk to suppress
  1072. // leading whitespace _BUT_ to output a single space before any non-whitespace.
  1073. // (This allows us to skip through multiple chunks' worth of whitespace, if
  1074. // necessary, yet still output the one required space if needed. The last block
  1075. // will aways have SUPPRESS_TRAILING_WS set, and so discard any remaining space.)
  1076. if(whiteSpaceFound && 0==(edgeTreatmentFlags&SUPPRESS_TRAILING_WS))
  1077. {
  1078. // handler.characters(SINGLE_SPACE, 0, 1); // Output single space
  1079. edgeTreatmentFlags |= SUPPRESS_LEADING_WS | CARRY_WS;
  1080. }
  1081. return edgeTreatmentFlags;
  1082. }
  1083. /**
  1084. * Directly normalize and dispatch the character array.
  1085. *
  1086. * @param ch The characters from the XML document.
  1087. * @param start The start position in the array.
  1088. * @param length The number of characters to read from the array.
  1089. * @param handler SAX ContentHandler object to receive the event.
  1090. * @exception org.xml.sax.SAXException Any SAX exception, possibly
  1091. * wrapping another exception.
  1092. */
  1093. public static void sendNormalizedSAXcharacters(char ch[],
  1094. int start, int length,
  1095. org.xml.sax.ContentHandler handler)
  1096. throws org.xml.sax.SAXException
  1097. {
  1098. sendNormalizedSAXcharacters(ch, start, length,
  1099. handler, SUPPRESS_BOTH);
  1100. }
  1101. /**
  1102. * Sends the specified range of characters as sax Comment.
  1103. * <p>
  1104. * Note that, unlike sendSAXcharacters, this has to be done as a single
  1105. * call to LexicalHandler#comment.
  1106. *
  1107. * @param ch SAX LexicalHandler object to receive the event.
  1108. * @param start Offset of first character in the range.
  1109. * @param length Number of characters to send.
  1110. * @exception org.xml.sax.SAXException may be thrown by handler's
  1111. * characters() method.
  1112. */
  1113. public void sendSAXComment(
  1114. org.xml.sax.ext.LexicalHandler ch, int start, int length)
  1115. throws org.xml.sax.SAXException
  1116. {
  1117. // %OPT% Do it this way for now...
  1118. String comment = getString(start, length);
  1119. ch.comment(comment.toCharArray(), 0, length);
  1120. }
  1121. /**
  1122. * Copies characters from this string into the destination character
  1123. * array.
  1124. *
  1125. * @param srcBegin index of the first character in the string
  1126. * to copy.
  1127. * @param srcEnd index after the last character in the string
  1128. * to copy.
  1129. * @param dst the destination array.
  1130. * @param dstBegin the start offset in the destination array.
  1131. * @exception IndexOutOfBoundsException If any of the following
  1132. * is true:
  1133. * <ul><li><code>srcBegin</code> is negative.
  1134. * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
  1135. * <li><code>srcEnd</code> is greater than the length of this
  1136. * string
  1137. * <li><code>dstBegin</code> is negative
  1138. * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
  1139. * <code>dst.length</code></ul>
  1140. * @exception NullPointerException if <code>dst</code> is <code>null</code>
  1141. */
  1142. private void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
  1143. {
  1144. // %TBD% Joe needs to write this function. Make public when implemented.
  1145. }
  1146. /**
  1147. * Encapsulation c'tor. After this is called, the source FastStringBuffer
  1148. * will be reset to use the new object as its m_innerFSB, and will have
  1149. * had its chunk size reset appropriately. IT SHOULD NEVER BE CALLED
  1150. * EXCEPT WHEN source.length()==1<<(source.m_chunkBits+source.m_rebundleBits)
  1151. *
  1152. * NEEDSDOC @param source
  1153. */
  1154. private FastStringBuffer(FastStringBuffer source)
  1155. {
  1156. // Copy existing information into new encapsulation
  1157. m_chunkBits = source.m_chunkBits;
  1158. m_maxChunkBits = source.m_maxChunkBits;
  1159. m_rebundleBits = source.m_rebundleBits;
  1160. m_chunkSize = source.m_chunkSize;
  1161. m_chunkMask = source.m_chunkMask;
  1162. m_array = source.m_array;
  1163. m_innerFSB = source.m_innerFSB;
  1164. // These have to be adjusted because we're calling just at the time
  1165. // when we would be about to allocate another chunk
  1166. m_lastChunk = source.m_lastChunk - 1;
  1167. m_firstFree = source.m_chunkSize;
  1168. // Establish capsule as the Inner FSB, reset chunk sizes/addressing
  1169. source.m_array = new char[16][];
  1170. source.m_innerFSB = this;
  1171. // Since we encapsulated just as we were about to append another
  1172. // chunk, return ready to create the chunk after the innerFSB
  1173. // -- 1, not 0.
  1174. source.m_lastChunk = 1;
  1175. source.m_firstFree = 0;
  1176. source.m_chunkBits += m_rebundleBits;
  1177. source.m_chunkSize = 1 << (source.m_chunkBits);
  1178. source.m_chunkMask = source.m_chunkSize - 1;
  1179. }
  1180. }