1 4 package com.tc.io.serializer; 5 6 import com.tc.io.TCDataOutput; 7 import com.tc.object.dna.impl.DNAEncoding; 8 9 import java.io.IOException ; 10 import java.io.ObjectOutput ; 11 import java.io.OutputStream ; 12 import java.io.UnsupportedEncodingException ; 13 14 32 public class TCObjectOutputStream implements ObjectOutput , TCDataOutput { 33 34 private static final DNAEncoding SERIALIZER_ENCODING = new DNAEncoding(DNAEncoding.SERIALIZER); 35 36 protected final OutputStream out; 37 38 public TCObjectOutputStream(OutputStream out) { 39 this.out = out; 40 } 41 42 48 public void writeObject(Object obj) { 49 if (obj != null && obj.getClass().getName().charAt(0) == '[') { 50 SERIALIZER_ENCODING.encodeArray(obj, this); 51 } else { 52 SERIALIZER_ENCODING.encode(obj, this); 53 } 54 } 55 56 62 public void writeString(String string) { 63 if (string == null) { 64 writeInt(-1); 65 return; 66 } 67 try { 68 byte strbytes[] = string.getBytes("UTF-8"); 69 writeInt(strbytes.length); 70 write(strbytes); 71 } catch (UnsupportedEncodingException e) { 72 throw new AssertionError (e); 73 } 74 } 75 76 public void flush() { 77 try { 78 out.flush(); 79 } catch (IOException e) { 80 throw new AssertionError (e); 81 } 82 } 83 84 public void writeBytes(String s) { 85 int len = s.length(); 86 try { 87 for (int i = 0; i < len; i++) { 88 out.write((byte) s.charAt(i)); 89 } 90 } catch (IOException e) { 91 throw new AssertionError (e); 92 } 93 } 94 95 public void writeChars(String s) { 96 int len = s.length(); 97 try { 98 for (int i = 0; i < len; i++) { 99 int v = s.charAt(i); 100 out.write((v >>> 8) & 0xFF); 101 out.write((v >>> 0) & 0xFF); 102 } 103 } catch (IOException e) { 104 throw new AssertionError (e); 105 } 106 } 107 108 113 public void writeUTF(String str) { 114 writeString(str); 115 } 116 117 public void close() { 118 flush(); 119 try { 120 out.close(); 121 } catch (IOException e) { 122 throw new AssertionError (e); 123 } 124 } 125 126 public void write(int b) { 127 try { 128 out.write(b); 129 } catch (IOException e) { 130 throw new AssertionError (e); 131 } 132 } 133 134 public void write(byte[] value) { 135 write(value, 0, value.length); 136 } 137 138 public void write(byte[] value, int offset, int length) { 139 try { 140 out.write(value, offset, length); 141 } catch (IOException e) { 142 throw new AssertionError (e); 143 } 144 } 145 146 public void writeBoolean(boolean value) { 147 try { 148 out.write(value ? 1 : 0); 149 } catch (IOException e) { 150 throw new AssertionError (e); 151 } 152 } 153 154 public void writeByte(int value) { 155 try { 156 out.write(value); 157 } catch (IOException e) { 158 throw new AssertionError (e); 159 } 160 } 161 162 public void writeChar(int v) { 163 try { 164 out.write((v >>> 8) & 0xFF); 165 out.write((v >>> 0) & 0xFF); 166 } catch (IOException e) { 167 throw new AssertionError (e); 168 } 169 } 170 171 public void writeDouble(double value) { 172 writeLong(Double.doubleToLongBits(value)); 173 } 174 175 public void writeFloat(float value) { 176 writeInt(Float.floatToIntBits(value)); 177 } 178 179 public void writeInt(int v) { 180 try { 181 out.write((v >>> 24) & 0xFF); 182 out.write((v >>> 16) & 0xFF); 183 out.write((v >>> 8) & 0xFF); 184 out.write((v >>> 0) & 0xFF); 185 } catch (IOException e) { 186 throw new AssertionError (e); 187 } 188 189 } 190 191 public void writeLong(long v) { 192 final byte writeBuffer[] = new byte[8]; 193 writeBuffer[0] = (byte) (v >>> 56); 194 writeBuffer[1] = (byte) (v >>> 48); 195 writeBuffer[2] = (byte) (v >>> 40); 196 writeBuffer[3] = (byte) (v >>> 32); 197 writeBuffer[4] = (byte) (v >>> 24); 198 writeBuffer[5] = (byte) (v >>> 16); 199 writeBuffer[6] = (byte) (v >>> 8); 200 writeBuffer[7] = (byte) (v >>> 0); 201 try { 202 out.write(writeBuffer, 0, 8); 203 } catch (IOException e) { 204 throw new AssertionError (e); 205 } 206 } 207 208 public void writeShort(int v) { 209 try { 210 out.write((v >>> 8) & 0xFF); 211 out.write((v >>> 0) & 0xFF); 212 } catch (IOException e) { 213 throw new AssertionError (e); 214 } 215 } 216 217 } 218
| Popular Tags
|