@Override public int read() throws IOException { return delegate.read(); }
final int nextByte = streamIn.read();
@Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); clusterName = ClusterName.readClusterName(in); clusterState = ClusterState.Builder.readFrom(in, null); int n = in.read(); files = new ArrayList(); for (int i = 0; i < n; i++) { files.set(i, new File(in.readString())); } }
@Override public void readFrom(StreamInput in) throws IOException { name = in.readString(); size = in.readInt(); int n; if (in.readBoolean()) { int capacity = in.readInt(); this.summary = new StreamSummary<>(capacity); n = in.readInt(); byte[] bytes = new byte[n]; in.read(bytes); try { summary.fromBytes(bytes); } catch (ClassNotFoundException e) { throw new IOException("Failed to build summary", e); } } n = in.readInt(); this.buckets = new ArrayList<>(); this.bucketsMap = null; for (int i = 0; i < n; ++i) { this.buckets.add(Bucket.readFrom(in)); } // buckets are alreaduy sorted here }
private void readMessage(MockChannel mockChannel, StreamInput input) throws IOException { Socket socket = mockChannel.activeChannel; byte[] minimalHeader = new byte[TcpHeader.MARKER_BYTES_SIZE]; int firstByte = input.read(); if (firstByte == -1) { throw new IOException("Connection reset by peer"); } minimalHeader[0] = (byte) firstByte; minimalHeader[1] = (byte) input.read(); int msgSize = input.readInt(); if (msgSize == -1) { socket.getOutputStream().flush(); } else { BytesStreamOutput output = new BytesStreamOutput(); final byte[] buffer = new byte[msgSize]; input.readFully(buffer); output.write(minimalHeader); output.writeInt(msgSize); output.write(buffer); final BytesReference bytes = output.bytes(); if (TcpTransport.validateMessageHeader(bytes)) { InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress(); messageReceived(bytes.slice(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE, msgSize), mockChannel, mockChannel.profile, remoteAddress, msgSize); } else { // ping message - we just drop all stuff } } }
public void testRandomReads() throws IOException { int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20)); BytesReference pbr = newBytesReference(length); StreamInput streamInput = pbr.streamInput(); BytesRefBuilder target = new BytesRefBuilder(); while (target.length() < pbr.length()) { switch (randomIntBetween(0, 10)) { case 6: case 5: target.append(new BytesRef(new byte[]{streamInput.readByte()})); break; case 4: case 3: BytesRef bytesRef = streamInput.readBytesRef(scaledRandomIntBetween(1, pbr.length() - target.length())); target.append(bytesRef); break; default: byte[] buffer = new byte[scaledRandomIntBetween(1, pbr.length() - target.length())]; int offset = scaledRandomIntBetween(0, buffer.length - 1); int read = streamInput.read(buffer, offset, buffer.length - offset); target.append(new BytesRef(buffer, offset, read)); break; } } assertEquals(pbr.length(), target.length()); BytesRef targetBytes = target.get(); assertArrayEquals(BytesReference.toBytes(pbr), Arrays.copyOfRange(targetBytes.bytes, targetBytes.offset, targetBytes.length)); }
byte[] buffer = new byte[sliceLength + scaledRandomIntBetween(1, 100)]; int offset = scaledRandomIntBetween(0, Math.max(1, buffer.length - sliceLength - 1)); int read = sliceInput.read(buffer, offset, sliceLength / 2); assertEquals(sliceInput.available(), sliceLength - read); sliceInput.read(buffer, offset + read, sliceLength - read); assertArrayEquals(sliceBytes, Arrays.copyOfRange(buffer, offset, offset + sliceLength)); assertEquals(sliceInput.available(), 0);
protected void validateRequest(StreamInput stream, long requestId, String action) throws IOException { final int nextByte = stream.read(); // calling read() is useful to make sure the message is fully read, even if there some kind of EOS marker if (nextByte != -1) { throw new IllegalStateException("Message not fully read (request) for requestId [" + requestId + "], action [" + action + "], available [" + stream.available() + "]; resetting"); } }
int len = 0; while (len < headerBytes.length) { final int read = in.read(headerBytes, len, headerBytes.length - len); if (read == -1) { break;
void handleHandshake(Version version, Set<String> features, TcpChannel channel, long requestId, StreamInput stream) throws IOException { // Must read the handshake request to exhaust the stream HandshakeRequest handshakeRequest = new HandshakeRequest(stream); final int nextByte = stream.read(); if (nextByte != -1) { throw new IllegalStateException("Handshake request not fully read for requestId [" + requestId + "], action [" + TransportHandshaker.HANDSHAKE_ACTION_NAME + "], available [" + stream.available() + "]; resetting"); } HandshakeResponse response = new HandshakeResponse(this.version); handshakeResponseSender.sendResponse(version, features, channel, response, requestId); }
@Override public int read() throws IOException { return delegate.read(); }
@Override public int read() throws IOException { return delegate.read(); }
@Override public int read() throws IOException { return delegate.read(); }
@Override public int read() throws IOException { return delegate.read(); }
protected void validateRequest(StreamInput stream, long requestId, String action) throws IOException { final int nextByte = stream.read(); // calling read() is useful to make sure the message is fully read, even if there some kind of EOS marker if (nextByte != -1) { throw new IllegalStateException("Message not fully read (request) for requestId [" + requestId + "], action [" + action + "], available [" + stream.available() + "]; resetting"); } }
protected void validateRequest(StreamInput stream, long requestId, String action) throws IOException { final int nextByte = stream.read(); // calling read() is useful to make sure the message is fully read, even if there some kind of EOS marker if (nextByte != -1) { throw new IllegalStateException("Message not fully read (request) for requestId [" + requestId + "], action [" + action + "], available [" + stream.available() + "]; resetting"); } }
int len = 0; while (len < headerBytes.length) { final int read = in.read(headerBytes, len, headerBytes.length - len); if (read == -1) { break;
int len = 0; while (len < headerBytes.length) { final int read = in.read(headerBytes, len, headerBytes.length - len); if (read == -1) { break;
/** * Validates that a response has been fully read (not too few bytes but also not too many bytes). * * @param stream A stream that is associated with the buffer that is tracked by this marker. * @param requestId The corresponding request id for this response. * @param handler The current response handler. * @param error Whether validate an error response. * @throws IOException Iff the stream could not be read. * @throws IllegalStateException Iff the request has not been fully read. */ public void validateResponse(StreamInput stream, long requestId, TransportResponseHandler<?> handler, boolean error) throws IOException { // Check the entire message has been read final int nextByte = stream.read(); // calling read() is useful to make sure the message is fully read, even if there is an EOS marker if (nextByte != -1) { throw new IllegalStateException("Message not fully read (response) for requestId [" + requestId + "], handler [" + handler + "], error [" + error + "]; resetting"); } if (buffer.readerIndex() < expectedReaderIndex) { throw new IllegalStateException("Message is fully read (response), yet there are " + (expectedReaderIndex - buffer.readerIndex()) + " remaining bytes; resetting"); } if (buffer.readerIndex() > expectedReaderIndex) { throw new IllegalStateException("Message read past expected size (response) for requestId [" + requestId + "], handler [" + handler + "], error [" + error + "]; resetting"); } } }
/** * Validates that a request has been fully read (not too few bytes but also not too many bytes). * * @param stream A stream that is associated with the buffer that is tracked by this marker. * @param requestId The current request id. * @param action The currently executed action. * @throws IOException Iff the stream could not be read. * @throws IllegalStateException Iff the request has not been fully read. */ public void validateRequest(StreamInput stream, long requestId, String action) throws IOException { final int nextByte = stream.read(); // calling read() is useful to make sure the message is fully read, even if there some kind of EOS marker if (nextByte != -1) { throw new IllegalStateException("Message not fully read (request) for requestId [" + requestId + "], action [" + action + "], readerIndex [" + buffer.readerIndex() + "] vs expected [" + expectedReaderIndex + "]; resetting"); } if (buffer.readerIndex() < expectedReaderIndex) { throw new IllegalStateException("Message is fully read (request), yet there are " + (expectedReaderIndex - buffer.readerIndex()) + " remaining bytes; resetting"); } if (buffer.readerIndex() > expectedReaderIndex) { throw new IllegalStateException( "Message read past expected size (request) for requestId [" + requestId + "], action [" + action + "], readerIndex [" + buffer.readerIndex() + "] vs expected [" + expectedReaderIndex + "]; resetting"); } }