1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.vfs.WriteStream; 33 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.InputStreamReader ; 37 import java.io.Reader ; 38 import java.io.UnsupportedEncodingException ; 39 import java.util.IdentityHashMap ; 40 41 44 abstract public class BinaryValue extends StringValue { 45 48 @Override  49 public BinaryValue toBinaryValue(Env env) 50 { 51 return this; 52 } 53 54 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 <Value, String > valueSet) 89 throws IOException  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 113 @Override  114 public InputStream toInputStream(String charset) 115 throws UnsupportedEncodingException  116 { 117 return toInputStream(); 118 } 119 120 124 @Override  125 public Reader toReader(String charset) 126 throws UnsupportedEncodingException  127 { 128 return new InputStreamReader (toInputStream(), charset); 129 } 130 131 134 @Override  135 public StringValue toStringBuilder() 136 { 137 BinaryBuilderValue bb = new BinaryBuilderValue(); 138 139 bb.append(this); 140 141 return bb; 142 } 143 144 150 @Override  151 public BinaryValue toBinaryValue(Env env, String charset) 152 { 153 return this; 154 } 155 156 161 @Override  162 public UnicodeValue toUnicodeValue(Env env) 163 { 164 String encoding = env.getRuntimeEncoding().toString(); 165 166 return toUnicodeValue(env, encoding); 167 } 168 169 172 @Override  173 public boolean isBinary() 174 { 175 return true; 176 } 177 178 abstract public byte[] toBytes(); 179 } 180 181 | Popular Tags |