KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > TempBufferStringValue


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
37 import java.io.Serializable JavaDoc;
38
39 /**
40  * Represents a PHP string value implemented as a TempBuffer, with
41  * encoding iso-8859-1..
42  */

43 public class TempBufferStringValue extends BinaryValue
44   implements Serializable JavaDoc
45 {
46   private TempBuffer _head;
47
48   private String JavaDoc _string;
49
50   public TempBufferStringValue(TempBuffer buffer)
51   {
52     _head = buffer;
53   }
54
55   /**
56    *
57    * @return _head as inputstream
58    */

59   public InputStream toInputStream()
60   {
61     TempReadStream ts = new TempReadStream(_head);
62     ts.setFreeWhenDone(false);
63     
64     return new StreamImplInputStream(ts);
65   }
66
67   //
68
// CharSegment
69
//
70

71   /**
72    * Returns the length as a string.
73    */

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   /**
86    * Returns the character at a given position
87    */

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   /**
106    * Prints the value.
107    *
108    * @param env
109    */

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   /**
118    * Converts to a string.
119    */

120   public String JavaDoc 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 JavaDoc(cbuf);
136     }
137
138     return _string;
139   }
140
141   /**
142    * Calculate the hash code
143    */

144   public int hashCode()
145   {
146     // Matches hashCode calculated in StringValue
147

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   /**
162    * Test for equality.
163    */

164   public boolean equals(Object JavaDoc 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   //
215
// Java Serialization
216
//
217

218   public Object JavaDoc writeReplace()
219   {
220     return new BinaryBuilderValue(toBytes());
221   }
222 }
223
224
Popular Tags