1 4 package com.tc.io.serializer; 5 6 import com.tc.io.TCDataInput; 7 import com.tc.object.dna.impl.DNAEncoding; 8 9 import java.io.EOFException ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.ObjectInput ; 13 14 32 public class TCObjectInputStream implements ObjectInput , TCDataInput { 33 34 private static final DNAEncoding SERIALIZER_ENCODING = new DNAEncoding(DNAEncoding.SERIALIZER); 35 36 private final InputStream in; 37 38 public TCObjectInputStream(InputStream in) { 39 this.in = in; 40 } 41 42 48 public Object readObject() throws ClassNotFoundException , IOException { 49 return SERIALIZER_ENCODING.decode(this); 50 } 51 52 public int read() throws IOException { 53 return in.read(); 54 } 55 56 public int read(byte[] b) throws IOException { 57 return in.read(b); 58 } 59 60 public int read(byte[] b, int off, int len) throws IOException { 61 return in.read(b, off, len); 62 } 63 64 public long skip(long n) throws IOException { 65 return in.skip(n); 66 } 67 68 public int available() throws IOException { 69 return in.available(); 70 } 71 72 public void close() throws IOException { 73 in.close(); 74 } 75 76 public void readFully(byte[] b) throws IOException { 77 readFully(b, 0, b.length); 78 } 79 80 public void readFully(byte[] b, int off, int len) throws IOException { 81 if (len < 0) throw new IndexOutOfBoundsException (); 82 int n = 0; 83 while (n < len) { 84 int count = in.read(b, off + n, len - n); 85 if (count < 0) throw new EOFException (); 86 n += count; 87 } 88 } 89 90 public int skipBytes(int n) throws IOException { 91 int total = 0; 92 int cur = 0; 93 94 while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) { 95 total += cur; 96 } 97 98 return total; 99 } 100 101 public boolean readBoolean() throws IOException { 102 int ch = in.read(); 103 if (ch < 0) throw new EOFException (); 104 return (ch != 0); 105 } 106 107 public byte readByte() throws IOException { 108 int ch = in.read(); 109 if (ch < 0) throw new EOFException (); 110 return (byte) (ch); 111 } 112 113 public int readUnsignedByte() throws IOException { 114 int ch = in.read(); 115 if (ch < 0) throw new EOFException (); 116 return ch; 117 } 118 119 public short readShort() throws IOException { 120 int ch1 = in.read(); 121 int ch2 = in.read(); 122 if ((ch1 | ch2) < 0) throw new EOFException (); 123 return (short) ((ch1 << 8) + (ch2 << 0)); 124 } 125 126 public int readUnsignedShort() throws IOException { 127 int ch1 = in.read(); 128 int ch2 = in.read(); 129 if ((ch1 | ch2) < 0) throw new EOFException (); 130 return (ch1 << 8) + (ch2 << 0); 131 } 132 133 public char readChar() throws IOException { 134 int ch1 = in.read(); 135 int ch2 = in.read(); 136 if ((ch1 | ch2) < 0) throw new EOFException (); 137 return (char) ((ch1 << 8) + (ch2 << 0)); 138 } 139 140 public int readInt() throws IOException { 141 int ch1 = in.read(); 142 int ch2 = in.read(); 143 int ch3 = in.read(); 144 int ch4 = in.read(); 145 if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException (); 146 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 147 } 148 149 public long readLong() throws IOException { 150 final byte readBuffer[] = new byte[8]; 151 readFully(readBuffer, 0, 8); 152 return (((long) readBuffer[0] << 56) + ((long) (readBuffer[1] & 255) << 48) + ((long) (readBuffer[2] & 255) << 40) 153 + ((long) (readBuffer[3] & 255) << 32) + ((long) (readBuffer[4] & 255) << 24) 154 + ((readBuffer[5] & 255) << 16) + ((readBuffer[6] & 255) << 8) + ((readBuffer[7] & 255) << 0)); 155 } 156 157 public float readFloat() throws IOException { 158 return Float.intBitsToFloat(readInt()); 159 } 160 161 public double readDouble() throws IOException { 162 return Double.longBitsToDouble(readLong()); 163 } 164 165 public String readLine() { 166 throw new UnsupportedOperationException ("Use BufferedReader instead."); 167 } 168 169 174 public String readUTF() throws IOException { 175 return readString(); 176 } 177 178 184 public String readString() throws IOException { 185 int len = readInt(); 186 if (len < 0) return null; 187 else if (len == 0) return ""; 188 final byte strbytes[] = new byte[len]; 189 readFully(strbytes); 190 return new String (strbytes, "UTF-8"); 191 } 192 193 } 194 | Popular Tags |