1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 33 public class TempCharStream extends StreamImpl { 34 private TempCharBuffer _head; 35 private TempCharBuffer _tail; 36 37 public TempCharStream() 38 { 39 } 40 41 44 public void openWrite() 45 { 46 TempCharBuffer ptr = _head; 47 48 _head = null; 49 _tail = null; 50 51 TempCharBuffer.freeAll(ptr); 52 } 53 54 57 public char []getTail() 58 { 59 return _tail.getBuffer(); 60 } 61 62 65 public TempCharBuffer getHead() 66 { 67 return _head; 68 } 69 70 73 public boolean canWrite() 74 { 75 return true; 76 } 77 78 public void write(char []buf, int offset, int length) 79 throws IOException 80 { 81 int index = 0; 82 while (index < length) { 83 if (_tail == null) 84 addBuffer(TempCharBuffer.allocate()); 85 else if (_tail._buf.length <= _tail._length) { 86 addBuffer(TempCharBuffer.allocate()); 87 88 } 90 91 int sublen = _tail._buf.length - _tail._length; 92 if (length - index < sublen) 93 sublen = length - index; 94 95 System.arraycopy(buf, index + offset, _tail._buf, _tail._length, sublen); 96 97 index += sublen; 98 _tail._length += sublen; 99 } 100 } 101 102 105 public void write(String s, int offset, int length) 106 throws IOException 107 { 108 while (length > 0) { 109 if (_tail == null) 110 addBuffer(TempCharBuffer.allocate()); 111 else if (_tail._buf.length <= _tail._length) { 112 addBuffer(TempCharBuffer.allocate()); 113 114 } 116 117 int sublen = _tail._buf.length - _tail._length; 118 if (length < sublen) 119 sublen = length; 120 121 s.getChars(offset, offset + sublen, _tail._buf, _tail._length); 122 123 offset += sublen; 124 length -= sublen; 125 _tail._length += sublen; 126 } 127 } 128 129 public void write(int ch) 130 throws IOException 131 { 132 if (_tail == null) 133 addBuffer(TempCharBuffer.allocate()); 134 else if (_tail._buf.length <= _tail._length) { 135 addBuffer(TempCharBuffer.allocate()); 136 137 } 139 140 _tail._buf[_tail._length++] = (char) ch; 141 } 142 143 private void addBuffer(TempCharBuffer buf) 144 { 145 buf._next = null; 146 if (_tail != null) { 147 _tail._next = buf; 148 _tail = buf; 149 } else { 150 _tail = buf; 151 _head = buf; 152 } 153 154 _head._bufferCount++; 155 } 156 157 public void flush() 158 throws IOException 159 { 160 } 161 162 public void close() 163 throws IOException 164 { 165 super.close(); 166 } 167 168 171 public int getLength() 172 { 173 int length = 0; 174 175 for (TempCharBuffer ptr = _head; ptr != null; ptr = ptr._next) { 176 length += ptr.getLength(); 177 } 178 179 return length; 180 } 181 182 public void clearWrite() 183 { 184 TempCharBuffer ptr = _head; 185 186 _head = null; 187 _tail = null; 188 189 TempCharBuffer.freeAll(ptr); 190 } 191 192 public void discard() 193 { 194 _head = null; 195 _tail = null; 196 } 197 198 201 public void destroy() 202 { 203 try { 204 close(); 205 } catch (IOException e) { 206 } 207 208 TempCharBuffer ptr = _head; 209 210 _head = null; 211 _tail = null; 212 213 TempCharBuffer.freeAll(ptr); 214 } 215 } 216 | Popular Tags |