forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBuffer.java
More file actions
640 lines (573 loc) · 19.2 KB
/
TextBuffer.java
File metadata and controls
640 lines (573 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package org.codehaus.jackson.util;
import java.math.BigDecimal;
import java.util.ArrayList;
/**
* TextBuffer is a class similar to {@link StringBuffer}, with
* following differences:
*<ul>
* <li>TextBuffer uses segments character arrays, to avoid having
* to do additional array copies when array is not big enough.
* This means that only reallocating that is necessary is done only once:
* if and when caller
* wants to access contents in a linear array (char[], String).
* </li>
* <li>TextBuffer can also be initialized in "shared mode", in which
* it will just act as a wrapper to a single char array managed
* by another object (like parser that owns it)
* </li>
* <li>TextBuffer is not synchronized.
* </li>
* </ul>
*/
public final class TextBuffer
{
final static char[] NO_CHARS = new char[0];
/**
* Let's limit maximum segment length to something sensible
* like 256k
*/
final static int MAX_SEGMENT_LEN = 0x40000;
/*
//////////////////////////////
// Configuration:
//////////////////////////////
*/
private final BufferRecycler _allocator;
/*
//////////////////////////////
// Shared input buffers
//////////////////////////////
*/
/**
* Shared input buffer; stored here in case some input can be returned
* as is, without being copied to collector's own buffers. Note that
* this is read-only for this Object.
*/
private char[] _inputBuffer;
/**
* Character offset of first char in input buffer; -1 to indicate
* that input buffer currently does not contain any useful char data
*/
private int _inputStart;
private int _inputLen;
/*
//////////////////////////////////////////////////
// Aggregation segments (when not using input buf)
//////////////////////////////////////////////////
*/
/**
* List of segments prior to currently active segment.
*/
private ArrayList<char[]> _segments;
/**
* Flag that indicates whether _seqments is non-empty
*/
private boolean _hasSegments = false;
// // // Currently used segment; not (yet) contained in _seqments
/**
* Amount of characters in segments in {@link _segments}
*/
private int _segmentSize;
private char[] _currentSegment;
/**
* Number of characters in currently active (last) segment
*/
private int _currentSize;
/*
//////////////////////////////////////////////////
// Caching of results
//////////////////////////////////////////////////
*/
/**
* String that will be constructed when the whole contents are
* needed; will be temporarily stored in case asked for again.
*/
private String _resultString;
private char[] _resultArray;
/*
//////////////////////////////////////////////
// Life-cycle
//////////////////////////////////////////////
*/
public TextBuffer(BufferRecycler allocator)
{
_allocator = allocator;
}
/**
* Method called to indicate that the underlying buffers should now
* be recycled if they haven't yet been recycled. Although caller
* can still use this text buffer, it is not advisable to call this
* method if that is likely, since next time a buffer is needed,
* buffers need to reallocated.
* Note: calling this method automatically also clears contents
* of the buffer.
*/
public void releaseBuffers()
{
if (_allocator != null && _currentSegment != null) {
// First, let's get rid of all but the largest char array
resetWithEmpty();
// And then return that array
char[] buf = _currentSegment;
_currentSegment = null;
_allocator.releaseCharBuffer(BufferRecycler.CharBufferType.TEXT_BUFFER, buf);
}
}
/**
* Method called to clear out any content text buffer may have, and
* initializes buffer to use non-shared data.
*/
public void resetWithEmpty()
{
_inputBuffer = null;
_inputStart = -1; // indicates shared buffer not used
_inputLen = 0;
_resultString = null;
_resultArray = null;
// And then reset internal input buffers, if necessary:
if (_hasSegments) {
clearSegments();
}
_currentSize = 0;
}
/**
* Method called to initialize the buffer with a shared copy of data; this means that buffer will just have pointers
* to actual data.It also means that if anything is to be appended to the buffer, it will first have to unshare it
* (make a local copy).
*
* @param buf Undocumented.
* @param start Undocumented.
* @param len Undocumented.
*/
public void resetWithShared(char[] buf, int start, int len)
{
// First, let's clear intermediate values, if any:
_resultString = null;
_resultArray = null;
// Then let's mark things we need about input buffer
_inputBuffer = buf;
_inputStart = start;
_inputLen = len;
// And then reset internal input buffers, if necessary:
if (_hasSegments) {
clearSegments();
}
}
public void resetWithCopy(char[] buf, int start, int len)
{
_inputBuffer = null;
_inputStart = -1; // indicates shared buffer not used
_inputLen = 0;
_resultString = null;
_resultArray = null;
// And then reset internal input buffers, if necessary:
if (_hasSegments) {
clearSegments();
} else if (_currentSegment == null) {
_currentSegment = findBuffer(len);
}
_currentSize = _segmentSize = 0;
append(buf, start, len);
}
/**
* Helper method used to find a buffer to use, ideally one
* recycled earlier.
*/
private final char[] findBuffer(int needed)
{
return _allocator.allocCharBuffer(BufferRecycler.CharBufferType.TEXT_BUFFER, needed);
}
private final void clearSegments()
{
_hasSegments = false;
/* Let's start using _last_ segment from list; for one, it's
* the biggest one, and it's also most likely to be cached
*/
/* 28-Aug-2009, tatu: Actually, the current segment should
* be the biggest one, already
*/
//_currentSegment = _segments.get(_segments.size() - 1);
_segments.clear();
_currentSize = _segmentSize = 0;
}
/*
//////////////////////////////////////////////
// Accessors for implementing public interface
//////////////////////////////////////////////
*/
/**
* @return Number of characters currently stored by this collector
*/
public int size() {
if (_inputStart >= 0) { // shared copy from input buf
return _inputLen;
}
// local segmented buffers
return _segmentSize + _currentSize;
}
public int getTextOffset()
{
/* Only shared input buffer can have non-zero offset; buffer
* segments start at 0, and if we have to create a combo buffer,
* that too will start from beginning of the buffer
*/
return (_inputStart >= 0) ? _inputStart : 0;
}
public char[] getTextBuffer()
{
// Are we just using shared input buffer?
if (_inputStart >= 0) {
return _inputBuffer;
}
// Nope; but does it fit in just one segment?
if (!_hasSegments) {
return _currentSegment;
}
// Nope, need to have/create a non-segmented array and return it
return contentsAsArray();
}
/*
//////////////////////////////////////////////
// Other accessors:
//////////////////////////////////////////////
*/
public String contentsAsString()
{
if (_resultString == null) {
// Has array been requested? Can make a shortcut, if so:
if (_resultArray != null) {
_resultString = new String(_resultArray);
} else {
// Do we use shared array?
if (_inputStart >= 0) {
if (_inputLen < 1) {
return (_resultString = "");
}
_resultString = new String(_inputBuffer, _inputStart, _inputLen);
} else { // nope... need to copy
// But first, let's see if we have just one buffer
int segLen = _segmentSize;
int currLen = _currentSize;
if (segLen == 0) { // yup
_resultString = (currLen == 0) ? "" : new String(_currentSegment, 0, currLen);
} else { // no, need to combine
StringBuilder sb = new StringBuilder(segLen + currLen);
// First stored segments
if (_segments != null) {
for (int i = 0, len = _segments.size(); i < len; ++i) {
char[] curr = _segments.get(i);
sb.append(curr, 0, curr.length);
}
}
// And finally, current segment:
sb.append(_currentSegment, 0, _currentSize);
_resultString = sb.toString();
}
}
}
}
return _resultString;
}
public char[] contentsAsArray()
{
char[] result = _resultArray;
if (result == null) {
_resultArray = result = buildResultArray();
}
return result;
}
/**
* Convenience method for converting contents of the buffer
* into a {@link BigDecimal}.
* @return Undocumented.
*/
public BigDecimal contentsAsDecimal()
throws NumberFormatException
{
// Already got a pre-cut array?
if (_resultArray != null) {
return new BigDecimal(_resultArray);
}
// Or a shared buffer?
if (_inputStart >= 0) {
return new BigDecimal(_inputBuffer, _inputStart, _inputLen);
}
// Or if not, just a single buffer (the usual case)
if (_segmentSize == 0) {
return new BigDecimal(_currentSegment, 0, _currentSize);
}
// If not, let's just get it aggregated...
return new BigDecimal(contentsAsArray());
}
/**
* Convenience method for converting contents of the buffer
* into a Double value.
* @return Undocumented.
*/
public double contentsAsDouble()
throws NumberFormatException
{
return Double.parseDouble(contentsAsString());
}
/*
//////////////////////////////////////////////
// Public mutators:
//////////////////////////////////////////////
*/
/**
* Method called to make sure that buffer is not using shared input
* buffer; if it is, it will copy such contents to private buffer.
*/
public void ensureNotShared() {
if (_inputStart >= 0) {
unshare(16);
}
}
public void append(char c) {
// Using shared buffer so far?
if (_inputStart >= 0) {
unshare(16);
}
_resultString = null;
_resultArray = null;
// Room in current segment?
char[] curr = _currentSegment;
if (_currentSize >= curr.length) {
expand(1);
curr = _currentSegment;
}
curr[_currentSize++] = c;
}
public void append(char[] c, int start, int len)
{
// Can't append to shared buf (sanity check)
if (_inputStart >= 0) {
unshare(len);
}
_resultString = null;
_resultArray = null;
// Room in current segment?
char[] curr = _currentSegment;
int max = curr.length - _currentSize;
if (max >= len) {
System.arraycopy(c, start, curr, _currentSize, len);
_currentSize += len;
} else {
// No room for all, need to copy part(s):
if (max > 0) {
System.arraycopy(c, start, curr, _currentSize, max);
start += max;
len -= max;
}
// And then allocate new segment; we are guaranteed to now
// have enough room in segment.
expand(len); // note: curr != _currentSegment after this
System.arraycopy(c, start, _currentSegment, 0, len);
_currentSize = len;
}
}
public void append(String str, int offset, int len)
{
// Can't append to shared buf (sanity check)
if (_inputStart >= 0) {
unshare(len);
}
_resultString = null;
_resultArray = null;
// Room in current segment?
char[] curr = _currentSegment;
int max = curr.length - _currentSize;
if (max >= len) {
str.getChars(offset, offset+len, curr, _currentSize);
_currentSize += len;
} else {
// No room for all, need to copy part(s):
if (max > 0) {
str.getChars(offset, offset+max, curr, _currentSize);
len -= max;
offset += max;
}
/* And then allocate new segment; we are guaranteed to now
* have enough room in segment.
*/
expand(len);
str.getChars(offset, offset+len, _currentSegment, 0);
_currentSize = len;
}
}
/*
//////////////////////////////////////////////
// Raw access, for high-performance use:
//////////////////////////////////////////////
*/
public char[] getCurrentSegment()
{
/* Since the intention of the caller is to directly add stuff into
* buffers, we should NOT have anything in shared buffer... ie. may
* need to unshare contents.
*/
if (_inputStart >= 0) {
unshare(1);
} else {
char[] curr = _currentSegment;
if (curr == null) {
_currentSegment = findBuffer(0);
} else if (_currentSize >= curr.length) {
// Plus, we better have room for at least one more char
expand(1);
}
}
return _currentSegment;
}
public char[] emptyAndGetCurrentSegment()
{
resetWithEmpty();
char[] curr = _currentSegment;
if (curr == null) {
_currentSegment = curr = findBuffer(0);
}
return curr;
}
public int getCurrentSegmentSize() {
return _currentSize;
}
public void setCurrentLength(int len) {
_currentSize = len;
}
public char[] finishCurrentSegment()
{
if (_segments == null) {
_segments = new ArrayList<char[]>();
}
_hasSegments = true;
_segments.add(_currentSegment);
int oldLen = _currentSegment.length;
_segmentSize += oldLen;
// Let's grow segments by 50%
int newLen = Math.min(oldLen + (oldLen >> 1), MAX_SEGMENT_LEN);
char[] curr = _charArray(newLen);
_currentSize = 0;
_currentSegment = curr;
return curr;
}
/**
* Method called to expand size of the current segment, to accomodate for more contiguous content.Usually only used
* when parsing tokens like names.
*
* @return Undocumented.
*/
public char[] expandCurrentSegment()
{
char[] curr = _currentSegment;
// Let's grow by 50%
int len = curr.length;
// Must grow by at least 1 char, no matter what
int newLen = (len == MAX_SEGMENT_LEN) ?
(MAX_SEGMENT_LEN + 1) : Math.min(MAX_SEGMENT_LEN, len + (len >> 1));
_currentSegment = _charArray(newLen);
System.arraycopy(curr, 0, _currentSegment, 0, len);
return _currentSegment;
}
/*
//////////////////////////////////////////////
// Standard methods:
//////////////////////////////////////////////
*/
/**
* Note: calling this method may not be as efficient as calling
* {@link #contentsAsString}, since it's not guaranteed that resulting
* String is cached.
*/
@Override
public String toString() {
return contentsAsString();
}
/*
//////////////////////////////////////////////
// Internal methods:
//////////////////////////////////////////////
*/
/**
* Method called if/when we need to append content when we have been
* initialized to use shared buffer.
*/
private void unshare(int needExtra)
{
int sharedLen = _inputLen;
_inputLen = 0;
char[] inputBuf = _inputBuffer;
_inputBuffer = null;
int start = _inputStart;
_inputStart = -1;
// Is buffer big enough, or do we need to reallocate?
int needed = sharedLen+needExtra;
if (_currentSegment == null || needed > _currentSegment.length) {
_currentSegment = findBuffer(needed);
}
if (sharedLen > 0) {
System.arraycopy(inputBuf, start, _currentSegment, 0, sharedLen);
}
_segmentSize = 0;
_currentSize = sharedLen;
}
/**
* Method called when current segment is full, to allocate new
* segment.
*/
private void expand(int minNewSegmentSize)
{
// First, let's move current segment to segment list:
if (_segments == null) {
_segments = new ArrayList<char[]>();
}
char[] curr = _currentSegment;
_hasSegments = true;
_segments.add(curr);
_segmentSize += curr.length;
int oldLen = curr.length;
// Let's grow segments by 50% minimum
int sizeAddition = oldLen >> 1;
if (sizeAddition < minNewSegmentSize) {
sizeAddition = minNewSegmentSize;
}
curr = _charArray(Math.min(MAX_SEGMENT_LEN, oldLen + sizeAddition));
_currentSize = 0;
_currentSegment = curr;
}
private char[] buildResultArray()
{
if (_resultString != null) { // Can take a shortcut...
return _resultString.toCharArray();
}
char[] result;
// Do we use shared array?
if (_inputStart >= 0) {
if (_inputLen < 1) {
return NO_CHARS;
}
result = _charArray(_inputLen);
System.arraycopy(_inputBuffer, _inputStart, result, 0,
_inputLen);
} else { // nope
int size = size();
if (size < 1) {
return NO_CHARS;
}
int offset = 0;
result = _charArray(size);
if (_segments != null) {
for (int i = 0, len = _segments.size(); i < len; ++i) {
char[] curr = (char[]) _segments.get(i);
int currLen = curr.length;
System.arraycopy(curr, 0, result, offset, currLen);
offset += currLen;
}
}
System.arraycopy(_currentSegment, 0, result, offset, _currentSize);
}
return result;
}
private final char[] _charArray(int len) {
return new char[len];
}
}