187
183
* For output this is the end of the buffer.
189
185
public int getStart() {
193
189
public int getOffset() {
198
194
* Returns the start offset of the bytes.
200
196
public void setOffset(int off) {
205
201
* Returns the length of the bytes.
207
203
public int getLength() {
212
208
public int getEnd() {
216
212
public void setEnd( int i ) {
220
216
// -------------------- Adding data --------------------
222
218
public void append( char b )
227
// couldn't make space
228
if( limit >0 && end >= limit ) {
223
// couldn't make space
224
if( limit >0 && end >= limit ) {
234
230
public void append( CharChunk src )
237
append( src.getBuffer(), src.getOffset(), src.getLength());
233
append( src.getBuffer(), src.getOffset(), src.getLength());
240
236
/** Add data to the buffer
242
238
public void append( char src[], int off, int len )
245
// will grow, up to limit
241
// will grow, up to limit
248
// if we don't have limit: makeSpace can grow as it wants
250
// assert: makeSpace made enough space
251
System.arraycopy( src, off, buff, end, len );
244
// if we don't have limit: makeSpace can grow as it wants
246
// assert: makeSpace made enough space
247
System.arraycopy( src, off, buff, end, len );
256
252
// Optimize on a common case.
257
253
// If the source is going to fill up all the space in buffer, may
260
256
out.realWriteChars( src, off, len );
264
// if we have limit and we're below
265
if( len <= limit - end ) {
266
// makeSpace will grow the buffer to the limit,
268
System.arraycopy( src, off, buff, end, len );
274
// need more space than we can afford, need to flush
277
// the buffer is already at ( or bigger than ) limit
280
// If len-avail < length ( i.e. after we fill the buffer with
281
// what we can, the remaining will fit in the buffer ) we'll just
282
// copy the first part, flush, then copy the second part - 1 write
283
// and still have some space for more. We'll still have 2 writes, but
284
// we write more on the first.
286
if( len + end < 2 * limit ) {
287
/* If the request length exceeds the size of the output buffer,
288
flush the output buffer and then write the data directly.
289
We can't avoid 2 writes, but we can write more on the second
292
System.arraycopy(src, off, buff, end, avail);
297
System.arraycopy(src, off+avail, buff, end, len - avail);
300
} else { // len > buf.length + avail
301
// long write - flush the buffer and write the rest
302
// directly from source
305
out.realWriteChars( src, off, len );
260
// if we have limit and we're below
261
if( len <= limit - end ) {
262
// makeSpace will grow the buffer to the limit,
264
System.arraycopy( src, off, buff, end, len );
270
// need more space than we can afford, need to flush
273
// the buffer is already at ( or bigger than ) limit
276
// If len-avail < length ( i.e. after we fill the buffer with
277
// what we can, the remaining will fit in the buffer ) we'll just
278
// copy the first part, flush, then copy the second part - 1 write
279
// and still have some space for more. We'll still have 2 writes, but
280
// we write more on the first.
282
if( len + end < 2 * limit ) {
283
/* If the request length exceeds the size of the output buffer,
284
flush the output buffer and then write the data directly.
285
We can't avoid 2 writes, but we can write more on the second
288
System.arraycopy(src, off, buff, end, avail);
293
System.arraycopy(src, off+avail, buff, end, len - avail);
296
} else { // len > buf.length + avail
297
// long write - flush the buffer and write the rest
298
// directly from source
301
out.realWriteChars( src, off, len );
310
306
/** Add data to the buffer
312
308
public void append( StringBuffer sb )
317
// will grow, up to limit
320
// if we don't have limit: makeSpace can grow as it wants
322
// assert: makeSpace made enough space
323
sb.getChars(0, len, buff, end );
330
int sbEnd = off + len;
331
while (sbOff < sbEnd) {
332
int d = min(limit - end, sbEnd - sbOff);
333
sb.getChars( sbOff, sbOff+d, buff, end);
313
// will grow, up to limit
316
// if we don't have limit: makeSpace can grow as it wants
318
// assert: makeSpace made enough space
319
sb.getChars(0, len, buff, end );
326
int sbEnd = off + len;
327
while (sbOff < sbEnd) {
328
int d = min(limit - end, sbEnd - sbOff);
329
sb.getChars( sbOff, sbOff+d, buff, end);
341
337
/** Append a string to the buffer
347
343
/** Append a string to the buffer
349
345
public void append(String s, int off, int len) throws IOException {
352
// will grow, up to limit
355
// if we don't have limit: makeSpace can grow as it wants
357
// assert: makeSpace made enough space
358
s.getChars(off, off+len, buff, end );
364
int sEnd = off + len;
365
while (sOff < sEnd) {
366
int d = min(limit - end, sEnd - sOff);
367
s.getChars( sOff, sOff+d, buff, end);
348
// will grow, up to limit
351
// if we don't have limit: makeSpace can grow as it wants
353
// assert: makeSpace made enough space
354
s.getChars(off, off+len, buff, end );
360
int sEnd = off + len;
361
while (sOff < sEnd) {
362
int d = min(limit - end, sEnd - sOff);
363
s.getChars( sOff, sOff+d, buff, end);
375
371
// -------------------- Removing data from the buffer --------------------
432
428
public void flushBuffer()
437
throw new IOException( "Buffer overflow, no sink " + limit + " " +
440
out.realWriteChars( buff, start, end - start );
433
throw new IOException( "Buffer overflow, no sink " + limit + " " +
436
out.realWriteChars( buff, start, end - start );
444
440
/** Make space for len chars. If len is small, allocate
445
* a reserve space too. Never grow bigger than limit.
441
* a reserve space too. Never grow bigger than limit.
447
443
private void makeSpace(int count)
452
int desiredSize=end + count;
454
// Can't grow above the limit
456
desiredSize > limit) {
461
if( desiredSize < 256 ) desiredSize=256; // take a minimum
462
buff=new char[desiredSize];
465
// limit < buf.length ( the buffer is already big )
466
// or we already have space XXX
467
if( desiredSize <= buff.length) {
470
// grow in larger chunks
471
if( desiredSize < 2 * buff.length ) {
472
newSize= buff.length * 2;
474
newSize > limit ) newSize=limit;
475
tmp=new char[newSize];
477
newSize= buff.length * 2 + count ;
479
newSize > limit ) newSize=limit;
480
tmp=new char[newSize];
483
System.arraycopy(buff, 0, tmp, 0, end);
448
int desiredSize=end + count;
450
// Can't grow above the limit
452
desiredSize > limit) {
457
if( desiredSize < 256 ) desiredSize=256; // take a minimum
458
buff=new char[desiredSize];
461
// limit < buf.length ( the buffer is already big )
462
// or we already have space XXX
463
if( desiredSize <= buff.length) {
466
// grow in larger chunks
467
if( desiredSize < 2 * buff.length ) {
468
newSize= buff.length * 2;
470
newSize > limit ) newSize=limit;
471
tmp=new char[newSize];
473
newSize= buff.length * 2 + count ;
475
newSize > limit ) newSize=limit;
476
tmp=new char[newSize];
479
System.arraycopy(buff, 0, tmp, 0, end);
488
484
// -------------------- Conversion and getters --------------------
534
530
* @return true if the comparison succeeded, false otherwise
536
532
public boolean equalsIgnoreCase(String s) {
539
if (c == null || len != s.length()) {
543
for (int i = 0; i < len; i++) {
544
if (Ascii.toLower( c[off++] ) != Ascii.toLower( s.charAt(i))) {
535
if (c == null || len != s.length()) {
539
for (int i = 0; i < len; i++) {
540
if (Ascii.toLower( c[off++] ) != Ascii.toLower( s.charAt(i))) {
551
547
public boolean equals(CharChunk cc) {
552
return equals( cc.getChars(), cc.getOffset(), cc.getLength());
548
return equals( cc.getChars(), cc.getOffset(), cc.getLength());
555
551
public boolean equals(char b2[], int off2, int len2) {
557
if( b1==null && b2==null ) return true;
559
if (b1== null || b2==null || end-start != len2) {
565
if (b1[off1++] != b2[off2++]) {
553
if( b1==null && b2==null ) return true;
555
if (b1== null || b2==null || end-start != len2) {
561
if (b1[off1++] != b2[off2++]) {
572
568
public boolean equals(byte b2[], int off2, int len2) {
574
if( b2==null && b1==null ) return true;
570
if( b2==null && b1==null ) return true;
576
if (b1== null || b2==null || end-start != len2) {
583
if ( b1[off1++] != (char)b2[off2++]) {
572
if (b1== null || b2==null || end-start != len2) {
579
if ( b1[off1++] != (char)b2[off2++]) {
655
651
* @param c the character
657
653
public int indexOf(char c, int starting) {
658
int ret = indexOf( buff, start+starting, end, c );
659
return (ret >= start) ? ret - start : -1;
654
int ret = indexOf( buff, start+starting, end, c );
655
return (ret >= start) ? ret - start : -1;
662
658
public static int indexOf( char chars[], int off, int cend, char qq )
664
while( off < cend ) {
660
while( off < cend ) {
674
670
public int indexOf( String src, int srcOff, int srcLen, int myOff ) {
675
char first=src.charAt( srcOff );
671
char first=src.charAt( srcOff );
677
// Look for first char
678
int srcEnd = srcOff + srcLen;
673
// Look for first char
674
int srcEnd = srcOff + srcLen;
680
for( int i=myOff+start; i <= (end - srcLen); i++ ) {
681
if( buff[i] != first ) continue;
682
// found first char, now look for a match
676
for( int i=myOff+start; i <= (end - srcLen); i++ ) {
677
if( buff[i] != first ) continue;
678
// found first char, now look for a match
684
for( int srcPos=srcOff + 1; srcPos< srcEnd; ) {
680
for( int srcPos=srcOff + 1; srcPos< srcEnd; ) {
685
681
if( buff[myPos++] != src.charAt( srcPos++ ))
687
683
if( srcPos==srcEnd ) return i-start; // found it
693
689
// -------------------- utils
694
690
private int min(int a, int b) {
699
695
// Char sequence impl