1 29 30 package com.caucho.vfs; 31 32 import com.caucho.util.FreeList; 33 34 public class TempCharBuffer { 35 private static FreeList<TempCharBuffer> _freeList = 36 new FreeList<TempCharBuffer>(32); 37 38 public static final int SIZE = TempBuffer.SIZE; 39 40 TempCharBuffer _next; 41 final char []_buf; 42 private int _offset; 43 int _length; 44 int _bufferCount; 45 46 49 public TempCharBuffer(int size) 50 { 51 _buf = new char[size]; 52 } 53 54 57 public static TempCharBuffer allocate() 58 { 59 TempCharBuffer next = _freeList.allocate(); 60 61 if (next == null) 62 return new TempCharBuffer(SIZE); 63 64 next._next = null; 65 66 next._offset = 0; 67 next._length = 0; 68 next._bufferCount = 0; 69 70 return next; 71 } 72 73 76 public void clear() 77 { 78 _next = null; 79 80 _offset = 0; 81 _length = 0; 82 _bufferCount = 0; 83 } 84 85 88 public final char []getBuffer() 89 { 90 return _buf; 91 } 92 93 96 public final int getLength() 97 { 98 return _length; 99 } 100 101 public final void setLength(int length) 102 { 103 _length = length; 104 } 105 106 public final int getCapacity() 107 { 108 return _buf.length; 109 } 110 111 public int getAvailable() 112 { 113 return _buf.length - _length; 114 } 115 116 public final TempCharBuffer getNext() 117 { 118 return _next; 119 } 120 121 public final void setNext(TempCharBuffer next) 122 { 123 _next = next; 124 } 125 126 public int write(char []buf, int offset, int length) 127 { 128 char []thisBuf = _buf; 129 int thisLength = _length; 130 131 if (thisBuf.length - thisLength < length) 132 length = thisBuf.length - thisLength; 133 134 System.arraycopy(buf, offset, thisBuf, thisLength, length); 135 136 _length = thisLength + length; 137 138 return length; 139 } 140 141 144 public static void free(TempCharBuffer buf) 145 { 146 buf._next = null; 147 148 if (buf._buf.length == SIZE) 149 _freeList.free(buf); 150 } 151 152 public static void freeAll(TempCharBuffer buf) 153 { 154 while (buf != null) { 155 TempCharBuffer next = buf._next; 156 buf._next = null; 157 if (buf._buf.length == SIZE) 158 _freeList.free(buf); 159 buf = next; 160 } 161 } 162 } 163 | Popular Tags |