1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.vfs.StreamImplInputStream; 33 import com.caucho.vfs.TempBuffer; 34 import com.caucho.vfs.TempReadStream; 35 36 import java.io.InputStream ; 37 import java.io.Serializable ; 38 39 43 public class TempBufferStringValue extends BinaryValue 44 implements Serializable 45 { 46 private TempBuffer _head; 47 48 private String _string; 49 50 public TempBufferStringValue(TempBuffer buffer) 51 { 52 _head = buffer; 53 } 54 55 59 public InputStream toInputStream() 60 { 61 TempReadStream ts = new TempReadStream(_head); 62 ts.setFreeWhenDone(false); 63 64 return new StreamImplInputStream(ts); 65 } 66 67 71 74 public int length() 75 { 76 int len = 0; 77 78 for (TempBuffer ptr = _head; ptr != null; ptr = ptr.getNext()) { 79 len += ptr.getLength(); 80 } 81 82 return len; 83 } 84 85 88 public char charAt(int index) 89 { 90 int len = 0; 91 92 for (TempBuffer ptr = _head; ptr != null; ptr = ptr.getNext()) { 93 int sublen = ptr.getLength(); 94 95 if (index < len + sublen) { 96 return (char) (ptr.getBuffer()[index - len] & 0xff); 97 } 98 99 len += sublen; 100 } 101 102 return 0; 103 } 104 105 110 public void print(Env env) 111 { 112 for (TempBuffer ptr = _head; ptr != null; ptr = ptr.getNext()) { 113 env.write(ptr.getBuffer(), 0, ptr.getLength()); 114 } 115 } 116 117 120 public String toString() 121 { 122 if (_string == null) { 123 char []cbuf = new char[length()]; 124 125 int i = 0; 126 for (TempBuffer ptr = _head; ptr != null; ptr = ptr.getNext()) { 127 byte []buf = ptr.getBuffer(); 128 129 int len = ptr.getLength(); 130 131 for (int j = 0; j < len; j++) 132 cbuf[i++] = (char) (buf[j] & 0xff); 133 } 134 135 _string = new String (cbuf); 136 } 137 138 return _string; 139 } 140 141 144 public int hashCode() 145 { 146 148 int hash = 37; 149 150 for (TempBuffer ptr = _head; ptr != null; ptr = ptr.getNext()) { 151 byte []buffer = ptr.getBuffer(); 152 int length = ptr.getLength(); 153 154 for (int i = 0; i < length; i++) 155 hash = 65521 * hash + (buffer[i] & 0xff); 156 } 157 158 return hash; 159 } 160 161 164 public boolean equals(Object o) 165 { 166 if (this == o) 167 return true; 168 else if ((o instanceof TempBufferStringValue)) { 169 TempBufferStringValue tb = (TempBufferStringValue) o; 170 171 TempBuffer ptrA = _head; 172 TempBuffer ptrB = tb._head; 173 174 while (ptrA != null && ptrB != null) { 175 byte []bufferA = ptrA.getBuffer(); 176 int lengthA = ptrA.getLength(); 177 178 byte []bufferB = ptrB.getBuffer(); 179 int lengthB = ptrB.getLength(); 180 181 if (lengthA != lengthB) 182 return false; 183 184 while (--lengthA >= 0) { 185 if (bufferA[lengthA] != bufferB[lengthA]) 186 return false; 187 } 188 189 ptrA = ptrA.getNext(); 190 ptrB = ptrB.getNext(); 191 } 192 193 return ptrA == null && ptrB == null; 194 } 195 else 196 return super.equals(o); 197 } 198 199 public byte[] toBytes() 200 { 201 int len = 0; 202 203 byte []buffer = new byte[length()]; 204 205 for (TempBuffer ptr = _head; ptr != null; ptr = ptr.getNext()) { 206 System.arraycopy(ptr.getBuffer(), 0, buffer, len, ptr.getLength()); 207 208 len += ptr.getLength(); 209 } 210 211 return buffer; 212 } 213 214 218 public Object writeReplace() 219 { 220 return new BinaryBuilderValue(toBytes()); 221 } 222 } 223 224 | Popular Tags |