KickJava   Java API By Example, From Geeks To Geeks.

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


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.WriteStream;
33
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37 import java.io.Reader JavaDoc;
38 import java.io.UnsupportedEncodingException JavaDoc;
39 import java.util.IdentityHashMap JavaDoc;
40
41 /**
42  * Represents a 8-bit binary string value.
43  */

44 abstract public class BinaryValue extends StringValue {
45   /**
46    * Convert to a binary value.
47    */

48   @Override JavaDoc
49   public BinaryValue toBinaryValue(Env env)
50   {
51     return this;
52   }
53
54   /**
55    * Converts to a long.
56    */

57   public static long toLong(byte []buffer, int offset, int len)
58   {
59     if (len == 0)
60       return 0;
61
62     long value = 0;
63     long sign = 1;
64
65     int i = 0;
66     int end = offset + len;
67
68     if (buffer[offset] == '-') {
69       sign = -1;
70       offset++;
71     }
72
73     while (offset < end) {
74       int ch = buffer[offset++];
75
76       if ('0' <= ch && ch <= '9')
77         value = 10 * value + ch - '0';
78       else
79         return sign * value;
80     }
81
82     return value;
83   }
84
85   public void varDumpImpl(Env env,
86                           WriteStream out,
87                           int depth,
88                           IdentityHashMap JavaDoc<Value, String JavaDoc> valueSet)
89     throws IOException JavaDoc
90   {
91     int length = length();
92     
93     out.print("binary(" + length() + ") \"");
94
95     for (int i = 0; i < length; i++) {
96       char ch = charAt(i);
97
98       if (0x20 <= ch && ch < 0x7f)
99     out.print(charAt(i));
100       else if (ch == '\r' || ch == '\n' || ch == '\t')
101     out.print(charAt(i));
102       else
103     out.print("\\x" + Integer.toHexString(ch >> 4) + Integer.toHexString(ch % 16));
104     }
105
106     out.print("\"");
107   }
108
109   /**
110    * Returns a byte stream.
111    * @param charset ignored since BinaryValue has no set encoding
112    */

113   @Override JavaDoc
114   public InputStream JavaDoc toInputStream(String JavaDoc charset)
115     throws UnsupportedEncodingException JavaDoc
116   {
117     return toInputStream();
118   }
119
120   /**
121    * Returns a Unicode char stream.
122    * @param charset encoding of the StringValue
123    */

124   @Override JavaDoc
125   public Reader JavaDoc toReader(String JavaDoc charset)
126     throws UnsupportedEncodingException JavaDoc
127   {
128     return new InputStreamReader JavaDoc(toInputStream(), charset);
129   }
130
131   /**
132    * Converts to a string builder
133    */

134   @Override JavaDoc
135   public StringValue toStringBuilder()
136   {
137     BinaryBuilderValue bb = new BinaryBuilderValue();
138     
139     bb.append(this);
140     
141     return bb;
142   }
143
144   /**
145    * Converts to a BinaryValue in desired charset.
146    *
147    * @param env
148    * @param charset ignored since BinaryValue has no set encoding
149    */

150   @Override JavaDoc
151   public BinaryValue toBinaryValue(Env env, String JavaDoc charset)
152   {
153     return this;
154   }
155
156   /**
157    * Converts from default charset to a UnicodeValue.
158    *
159    * @param env
160    */

161   @Override JavaDoc
162   public UnicodeValue toUnicodeValue(Env env)
163   {
164     String JavaDoc encoding = env.getRuntimeEncoding().toString();
165
166     return toUnicodeValue(env, encoding);
167   }
168
169   /**
170    * Returns true for BinaryValue
171    */

172   @Override JavaDoc
173   public boolean isBinary()
174   {
175     return true;
176   }
177
178   abstract public byte[] toBytes();
179 }
180
181
Popular Tags