1 28 29 package com.caucho.vfs; 30 31 import java.io.Reader ; 32 33 36 public class TempCharReader extends Reader { 37 private TempCharBuffer _top; 38 private TempCharBuffer _head; 39 private char []_buffer; 40 private int _offset; 41 private int _length; 42 private boolean _isFree; 43 44 47 public TempCharReader() 48 { 49 } 50 51 54 public TempCharReader(TempCharBuffer head) 55 { 56 init(head); 57 } 58 59 62 public void setFree(boolean isFree) 63 { 64 _isFree = isFree; 65 } 66 67 70 public void init(TempCharBuffer head) 71 { 72 _top = head; 73 _head = head; 74 75 if (head != null) { 76 _buffer = head.getBuffer(); 77 _length = head.getLength(); 78 } 79 else 80 _length = 0; 81 82 _offset = 0; 83 } 84 85 88 public void reset() 89 { 90 init(_top); 91 } 92 93 96 public int read() 97 { 98 if (_length <= _offset) { 99 if (_head == null) 100 return -1; 101 102 TempCharBuffer next = _head.getNext(); 103 if (_isFree) 104 TempCharBuffer.free(_head); 105 _head = next; 106 107 if (_head == null) 108 return -1; 109 110 _buffer = _head.getBuffer(); 111 _length = _head.getLength(); 112 _offset = 0; 113 } 114 115 return _buffer[_offset++]; 116 } 117 118 121 public int read(char []buffer, int offset, int length) 122 { 123 int readLength = 0; 124 125 while (length > 0) { 126 if (_length <= _offset) { 127 if (_head == null) 128 return readLength == 0 ? -1 : readLength; 129 130 TempCharBuffer next = _head.getNext(); 131 if (_isFree) 132 TempCharBuffer.free(_head); 133 _head = next; 134 135 if (_head == null) 136 return readLength == 0 ? -1 : readLength; 137 138 _buffer = _head.getBuffer(); 139 _length = _head.getLength(); 140 _offset = 0; 141 } 142 143 int sublen = _length - _offset; 144 145 if (length < sublen) 146 sublen = length; 147 148 System.arraycopy(_buffer, _offset, buffer, offset, sublen); 149 150 _offset += sublen; 151 offset += sublen; 152 length -= sublen; 153 readLength += sublen; 154 } 155 156 return readLength; 157 } 158 159 162 public boolean isEmpty() 163 { 164 return _head == null; 165 } 166 167 170 public void close() 171 { 172 if (_isFree) 173 TempCharBuffer.freeAll(_head); 174 175 _head = null; 176 _buffer = null; 177 _offset = 0; 178 _length = 0; 179 } 180 } 181 | Popular Tags |