| 1 |
/*
|
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
| 3 |
* contributor license agreements. See the NOTICE file distributed with
|
| 4 |
* this work for additional information regarding copyright ownership.
|
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
| 6 |
* (the "License"); you may not use this file except in compliance with
|
| 7 |
* the License. You may obtain a copy of the License at
|
| 8 |
*
|
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
*
|
| 11 |
* Unless required by applicable law or agreed to in writing, software
|
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
* See the License for the specific language governing permissions and
|
| 15 |
* limitations under the License.
|
| 16 |
*/
|
| 17 |
|
| 18 |
package socket.io;
|
| 19 |
|
| 20 |
import java.io.DataInputStream;
|
| 21 |
import java.io.FilterInputStream;
|
| 22 |
import java.io.InputStream;
|
| 23 |
import java.io.IOException;
|
| 24 |
|
| 25 |
/**
|
| 26 |
* This input stream works in conjunction with the WrappedOutputStream
|
| 27 |
* to introduce a protocol for reading arbitrary length data in a
|
| 28 |
* uniform way.
|
| 29 |
* <p>
|
| 30 |
* <strong>Note:</strong> See the javadoc for WrappedOutputStream for
|
| 31 |
* more information.
|
| 32 |
*
|
| 33 |
* @see WrappedOutputStream
|
| 34 |
*
|
| 35 |
* @author Andy Clark, IBM
|
| 36 |
*
|
| 37 |
* @version $Id$
|
| 38 |
*/
|
| 39 |
public class WrappedInputStream
|
| 40 |
extends FilterInputStream {
|
| 41 |
|
| 42 |
//
|
| 43 |
// Data
|
| 44 |
//
|
| 45 |
|
| 46 |
/** Bytes left on input stream for current packet. */
|
| 47 |
protected int fPacketCount;
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Data input stream. This stream is used to input the block sizes
|
| 51 |
* from the data stream that are written by the WrappedOutputStream.
|
| 52 |
* <p>
|
| 53 |
* <strong>Note:</strong> The data input stream is only used for
|
| 54 |
* reading the byte count for performance reasons. We avoid the
|
| 55 |
* method indirection for reading the byte data.
|
| 56 |
*/
|
| 57 |
protected DataInputStream fDataInputStream;
|
| 58 |
|
| 59 |
/** To mark that the stream is "closed". */
|
| 60 |
protected boolean fClosed;
|
| 61 |
|
| 62 |
//
|
| 63 |
// Constructors
|
| 64 |
//
|
| 65 |
|
| 66 |
/** Constructs a wrapper for the given an input stream. */
|
| 67 |
public WrappedInputStream(InputStream stream) {
|
| 68 |
super(stream);
|
| 69 |
fDataInputStream = new DataInputStream(stream);
|
| 70 |
} // <init>(InputStream)
|
| 71 |
|
| 72 |
//
|
| 73 |
// InputStream methods
|
| 74 |
//
|
| 75 |
|
| 76 |
/** Reads a single byte. */
|
| 77 |
public int read() throws IOException {
|
| 78 |
|
| 79 |
// ignore, if already closed
|
| 80 |
if (fClosed) {
|
| 81 |
return -1;
|
| 82 |
}
|
| 83 |
|
| 84 |
// read packet header
|
| 85 |
if (fPacketCount == 0) {
|
| 86 |
fPacketCount = fDataInputStream.readInt() & 0x7FFFFFFF;
|
| 87 |
if (fPacketCount == 0) {
|
| 88 |
fClosed = true;
|
| 89 |
return -1;
|
| 90 |
}
|
| 91 |
}
|
| 92 |
|
| 93 |
// read a byte from the packet
|
| 94 |
fPacketCount--;
|
| 95 |
return super.in.read();
|
| 96 |
|
| 97 |
} // read():int
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Reads a block of bytes and returns the total number of bytes read.
|
| 101 |
*/
|
| 102 |
public int read(byte[] b, int offset, int length) throws IOException {
|
| 103 |
|
| 104 |
// ignore, if already closed
|
| 105 |
if (fClosed) {
|
| 106 |
return -1;
|
| 107 |
}
|
| 108 |
|
| 109 |
// read packet header
|
| 110 |
if (fPacketCount == 0) {
|
| 111 |
fPacketCount = fDataInputStream.readInt() & 0x7FFFFFFF;
|
| 112 |
if (fPacketCount == 0) {
|
| 113 |
fClosed = true;
|
| 114 |
return -1;
|
| 115 |
}
|
| 116 |
}
|
| 117 |
|
| 118 |
// read bytes from packet
|
| 119 |
if (length > fPacketCount) {
|
| 120 |
length = fPacketCount;
|
| 121 |
}
|
| 122 |
int count = super.in.read(b, offset, length);
|
| 123 |
if (count == -1) {
|
| 124 |
// NOTE: This condition should not happen. The end of
|
| 125 |
// the stream should always be designated by a
|
| 126 |
// byte count header of 0. -Ac
|
| 127 |
fClosed = true;
|
| 128 |
return -1;
|
| 129 |
}
|
| 130 |
fPacketCount -= count;
|
| 131 |
|
| 132 |
// return total bytes read
|
| 133 |
return count;
|
| 134 |
|
| 135 |
} // read(byte[],int,int):int
|
| 136 |
|
| 137 |
/** Skips the specified number of bytes from the input stream. */
|
| 138 |
public long skip(long n) throws IOException {
|
| 139 |
if (!fClosed) {
|
| 140 |
// NOTE: This should be rewritten to be more efficient. -Ac
|
| 141 |
for (long i = 0; i < n; i++) {
|
| 142 |
int b = read();
|
| 143 |
if (b == -1) {
|
| 144 |
return i + 1;
|
| 145 |
}
|
| 146 |
}
|
| 147 |
return n;
|
| 148 |
}
|
| 149 |
return 0;
|
| 150 |
} // skip(long):long
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Closes the input stream. This method will search for the end of
|
| 154 |
* the wrapped input, positioning the stream at after the end packet.
|
| 155 |
* <p>
|
| 156 |
* <strong>Note:</strong> This method does not close the underlying
|
| 157 |
* input stream.
|
| 158 |
*/
|
| 159 |
public void close() throws IOException {
|
| 160 |
if (!fClosed) {
|
| 161 |
fClosed = true;
|
| 162 |
do {
|
| 163 |
super.in.skip(fPacketCount);
|
| 164 |
fPacketCount = fDataInputStream.readInt() & 0x7FFFFFFF;
|
| 165 |
} while (fPacketCount > 0);
|
| 166 |
}
|
| 167 |
} // close()
|
| 168 |
|
| 169 |
} // class WrappedInputStream
|