1 16 17 package socket.io; 18 19 import java.io.DataInputStream ; 20 import java.io.FilterInputStream ; 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 24 38 public class WrappedInputStream 39 extends FilterInputStream { 40 41 45 46 protected int fPacketCount; 47 48 56 protected DataInputStream fDataInputStream; 57 58 59 protected boolean fClosed; 60 61 65 66 public WrappedInputStream(InputStream stream) { 67 super(stream); 68 fDataInputStream = new DataInputStream (stream); 69 } 71 75 76 public int read() throws IOException { 77 78 if (fClosed) { 80 return -1; 81 } 82 83 if (fPacketCount == 0) { 85 fPacketCount = fDataInputStream.readInt() & 0x7FFFFFFF; 86 if (fPacketCount == 0) { 87 fClosed = true; 88 return -1; 89 } 90 } 91 92 fPacketCount--; 94 return super.in.read(); 95 96 } 98 101 public int read(byte[] b, int offset, int length) throws IOException { 102 103 if (fClosed) { 105 return -1; 106 } 107 108 if (fPacketCount == 0) { 110 fPacketCount = fDataInputStream.readInt() & 0x7FFFFFFF; 111 if (fPacketCount == 0) { 112 fClosed = true; 113 return -1; 114 } 115 } 116 117 if (length > fPacketCount) { 119 length = fPacketCount; 120 } 121 int count = super.in.read(b, offset, length); 122 if (count == -1) { 123 fClosed = true; 127 return -1; 128 } 129 fPacketCount -= count; 130 131 return count; 133 134 } 136 137 public long skip(long n) throws IOException { 138 if (!fClosed) { 139 for (long i = 0; i < n; i++) { 141 int b = read(); 142 if (b == -1) { 143 return i + 1; 144 } 145 } 146 return n; 147 } 148 return 0; 149 } 151 158 public void close() throws IOException { 159 if (!fClosed) { 160 fClosed = true; 161 do { 162 super.in.skip(fPacketCount); 163 fPacketCount = fDataInputStream.readInt() & 0x7FFFFFFF; 164 } while (fPacketCount > 0); 165 } 166 } 168 } | Popular Tags |