1 19 20 package com.maverick.util; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 25 import java.math.BigInteger ; 26 27 32 public class ByteArrayWriter 33 extends ByteArrayOutputStream { 34 35 38 public ByteArrayWriter() { 39 40 } 41 42 46 public ByteArrayWriter(int length) { 47 super(length); 48 } 49 50 54 public byte[] array() { 55 return buf; 56 } 57 58 62 public void move(int numBytes) { 63 count += numBytes; 64 } 65 66 71 public void writeBigInteger(BigInteger bi) throws IOException { 72 byte[] raw = bi.toByteArray(); 73 74 writeInt(raw.length); 75 write(raw); 76 } 77 78 83 public void writeBoolean(boolean b) { 84 write(b ? 1 : 0); 85 } 86 87 92 public void writeBinaryString(byte[] data) throws IOException { 93 if(data==null) 94 writeInt(0); 95 else 96 writeBinaryString(data, 0, data.length); 97 } 98 99 106 public void writeBinaryString(byte[] data, int offset, int len) throws 107 IOException { 108 if(data==null) 109 writeInt(0); 110 else { 111 writeInt(len); 112 write(data, offset, len); 113 } 114 } 115 116 public void writeMPINT(BigInteger b) { 117 short bytes = (short) ( (b.bitLength() + 7) / 8); 118 byte[] raw = b.toByteArray(); 119 writeShort( (short) b.bitLength()); 120 if (raw[0] == 0) { 121 write(raw, 1, bytes); 122 } 123 else { 124 write(raw, 0, bytes); 125 } 126 } 127 128 public void writeShort(short s) { 129 write( (s >>> 8) & 0xFF); 130 write( (s >>> 0) & 0xFF); 131 } 132 133 138 public void writeInt(long i) throws IOException { 139 byte[] raw = new byte[4]; 140 141 raw[0] = (byte) (i >> 24); 142 raw[1] = (byte) (i >> 16); 143 raw[2] = (byte) (i >> 8); 144 raw[3] = (byte) (i); 145 146 write(raw); 147 } 148 149 154 public void writeInt(int i) throws IOException { 155 byte[] raw = new byte[4]; 156 157 raw[0] = (byte) (i >> 24); 158 raw[1] = (byte) (i >> 16); 159 raw[2] = (byte) (i >> 8); 160 raw[3] = (byte) (i); 161 162 write(raw); 163 } 164 165 170 public static byte[] encodeInt(int i) { 171 byte[] raw = new byte[4]; 172 raw[0] = (byte) (i >> 24); 173 raw[1] = (byte) (i >> 16); 174 raw[2] = (byte) (i >> 8); 175 raw[3] = (byte) (i); 176 return raw; 177 } 178 179 public static void encodeInt(byte[] buf, int off, int i) { 180 buf[off++] = (byte) (i >> 24); 181 buf[off++] = (byte) (i >> 16); 182 buf[off++] = (byte) (i >> 8); 183 buf[off] = (byte) (i); 184 } 185 186 public void writeUINT32(UnsignedInteger32 value) throws IOException { 187 writeInt(value.longValue()); 188 } 189 190 public void writeUINT64(UnsignedInteger64 value) throws IOException { 191 byte[] raw = new byte[8]; 192 byte[] bi = value.bigIntValue().toByteArray(); 193 System.arraycopy(bi, 0, raw, raw.length - bi.length, bi.length); 194 write(raw); 196 } 197 198 public void writeUINT64(long value) throws IOException { 199 writeUINT64(new UnsignedInteger64(value)); 200 } 201 202 214 215 220 public void writeString(String str) throws IOException { 221 writeString(str, ByteArrayReader.getCharsetEncoding()); 222 } 223 224 231 public void writeString(String str, String charset) throws IOException { 232 233 if (str == null) { 234 writeInt(0); 235 } 236 else { 237 byte[] tmp; 238 239 if(ByteArrayReader.encode) 240 tmp = str.getBytes(charset); 241 else 242 tmp = str.getBytes(); 243 244 writeInt(tmp.length); 245 write(tmp); 246 } 247 } 248 249 } 250 | Popular Tags |