You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2016/05/09 13:31:45 UTC
svn commit: r1742948 - in /httpcomponents/httpcore/trunk/httpcore5-h2/src:
main/java/org/apache/hc/core5/http2/
main/java/org/apache/hc/core5/http2/frame/
main/java/org/apache/hc/core5/http2/impl/io/
main/java/org/apache/hc/core5/http2/impl/nio/ main/j...
Author: olegk
Date: Mon May 9 13:31:45 2016
New Revision: 1742948
URL: http://svn.apache.org/viewvc?rev=1742948&view=rev
Log:
RFC 7540: frame input / output buffer implementations
Added:
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java (contents, props changed)
- copied, changed from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionInputBufferImpl.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputBuffer.java
- copied, changed from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java (with props)
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java (with props)
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java (contents, props changed)
- copied, changed from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestFrameInOutBuffers.java
- copied, changed from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java (contents, props changed)
- copied, changed from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java
Removed:
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionInputBufferImpl.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/io/SessionInputBuffer.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/io/SessionOutputBuffer.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java
Modified:
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2ConnectionException.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2StreamException.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/DefaultFrameFactory.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java
httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2ConnectionException.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2ConnectionException.java?rev=1742948&r1=1742947&r2=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2ConnectionException.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2ConnectionException.java Mon May 9 13:31:45 2016
@@ -26,37 +26,29 @@
*/
package org.apache.hc.core5.http2;
-import org.apache.hc.core5.http.ProtocolException;
+import java.io.IOException;
+
import org.apache.hc.core5.util.Args;
-public class H2ConnectionException extends ProtocolException {
+public class H2ConnectionException extends IOException {
private static final long serialVersionUID = -2014204317155428658L;
private final int code;
- private final long streamId;
- public H2ConnectionException(final H2Error error, final long streamId, final String message) {
+ public H2ConnectionException(final H2Error error, final String message) {
super(message);
Args.notNull(error, "H2 Error code may not be null");
- Args.positive(streamId, "H2 stream id may not be negative or zero");
this.code = error.getCode();
- this.streamId = streamId;
}
- public H2ConnectionException(final int code, final long streamId, final String message) {
+ public H2ConnectionException(final int code, final String message) {
super(message);
- Args.positive(streamId, "H2 stream id may not be negative or zero");
this.code = code;
- this.streamId = streamId;
}
public int getCode() {
return code;
}
- public long getStreamId() {
- return streamId;
- }
-
};
Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2StreamException.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2StreamException.java?rev=1742948&r1=1742947&r2=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2StreamException.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/H2StreamException.java Mon May 9 13:31:45 2016
@@ -26,10 +26,11 @@
*/
package org.apache.hc.core5.http2;
-import org.apache.hc.core5.http.ProtocolException;
+import java.io.IOException;
+
import org.apache.hc.core5.util.Args;
-public class H2StreamException extends ProtocolException {
+public class H2StreamException extends IOException {
private static final long serialVersionUID = 6321637486572232180L;
Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/DefaultFrameFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/DefaultFrameFactory.java?rev=1742948&r1=1742947&r2=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/DefaultFrameFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/DefaultFrameFactory.java Mon May 9 13:31:45 2016
@@ -33,22 +33,24 @@ import org.apache.hc.core5.util.Args;
public class DefaultFrameFactory extends FrameFactory {
- public Frame<ByteBuffer> createHeaders(final int streamId, final ByteBuffer content, final boolean endHeaders, final boolean endStream) {
+ public static final FrameFactory INSTANCE = new DefaultFrameFactory();
+
+ public Frame<ByteBuffer> createHeaders(final int streamId, final ByteBuffer payload, final boolean endHeaders, final boolean endStream) {
Args.positive(streamId, "Stream id");
final int flags = (endHeaders ? FrameFlag.END_HEADERS.value : 0) | (endStream ? FrameFlag.END_STREAM.value : 0);
- return new ByteBufferFrame(FrameType.HEADERS.getValue(), flags, streamId, content);
+ return new ByteBufferFrame(FrameType.HEADERS.getValue(), flags, streamId, payload);
}
- public Frame<ByteBuffer> createContinuation(final int streamId, final ByteBuffer content, final boolean endHeaders) {
+ public Frame<ByteBuffer> createContinuation(final int streamId, final ByteBuffer payload, final boolean endHeaders) {
Args.positive(streamId, "Stream id");
final int flags = (endHeaders ? FrameFlag.END_HEADERS.value : 0);
- return new ByteBufferFrame(FrameType.CONTINUATION.getValue(), flags, streamId, content);
+ return new ByteBufferFrame(FrameType.CONTINUATION.getValue(), flags, streamId, payload);
}
- public Frame<ByteBuffer> createData(final int streamId, final ByteBuffer content, final boolean endStream) {
+ public Frame<ByteBuffer> createData(final int streamId, final ByteBuffer payload, final boolean endStream) {
Args.positive(streamId, "Stream id");
final int flags = (endStream ? FrameFlag.END_STREAM.value : 0);
- return new ByteBufferFrame(FrameType.DATA.getValue(), flags, streamId, content);
+ return new ByteBufferFrame(FrameType.DATA.getValue(), flags, streamId, payload);
}
}
Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java?rev=1742948&r1=1742947&r2=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java Mon May 9 13:31:45 2016
@@ -59,18 +59,16 @@ public abstract class FrameFactory {
return new ByteBufferFrame(FrameType.RST_STREAM.getValue(), 0, streamId, payload);
}
- public Frame<ByteBuffer> createPing(final byte[] opaqueData) {
+ public Frame<ByteBuffer> createPing(final ByteBuffer opaqueData) {
Args.notNull(opaqueData, "Opaque data");
- Args.check(opaqueData.length == 8, "Opaque data length must be equal 8");
- final ByteBuffer payload = ByteBuffer.wrap(opaqueData);
- return new ByteBufferFrame(FrameType.PING.getValue(), 0, 0, payload);
+ Args.check(opaqueData.remaining() == 8, "Opaque data length must be equal 8");
+ return new ByteBufferFrame(FrameType.PING.getValue(), 0, 0, opaqueData);
}
- public Frame<ByteBuffer> createPingAck(final byte[] opaqueData) {
+ public Frame<ByteBuffer> createPingAck(final ByteBuffer opaqueData) {
Args.notNull(opaqueData, "Opaque data");
- Args.check(opaqueData.length == 8, "Opaque data length must be equal 8");
- final ByteBuffer payload = ByteBuffer.wrap(opaqueData);
- return new ByteBufferFrame(FrameType.PING.getValue(), FrameFlag.ACK.value, 0, payload);
+ Args.check(opaqueData.remaining() == 8, "Opaque data length must be equal 8");
+ return new ByteBufferFrame(FrameType.PING.getValue(), FrameFlag.ACK.value, 0, opaqueData);
}
public Frame<ByteBuffer> createGoAway(final int lastStream, final H2Error error, final String message) {
@@ -84,10 +82,10 @@ public abstract class FrameFactory {
return new ByteBufferFrame(FrameType.GOAWAY.getValue(), 0, 0, payload);
}
- public abstract Frame<ByteBuffer> createHeaders(int streamId, ByteBuffer content, boolean endHeaders, boolean endStream);
+ public abstract Frame<ByteBuffer> createHeaders(int streamId, ByteBuffer payload, boolean endHeaders, boolean endStream);
- public abstract Frame<ByteBuffer> createContinuation(int streamId, ByteBuffer content, boolean endHeaders);
+ public abstract Frame<ByteBuffer> createContinuation(int streamId, ByteBuffer payload, boolean endHeaders);
- public abstract Frame<ByteBuffer> createData(int streamId, ByteBuffer content, boolean endStream);
+ public abstract Frame<ByteBuffer> createData(int streamId, ByteBuffer payload, boolean endStream);
}
Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java (from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionInputBufferImpl.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionInputBufferImpl.java&r1=1742947&r2=1742948&rev=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionInputBufferImpl.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java Mon May 9 13:31:45 2016
@@ -33,7 +33,6 @@ import java.nio.ByteBuffer;
import org.apache.hc.core5.annotation.NotThreadSafe;
import org.apache.hc.core5.http.ConnectionClosedException;
-import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http2.H2ConnectionException;
import org.apache.hc.core5.http2.H2CorruptFrameException;
import org.apache.hc.core5.http2.H2Error;
@@ -43,15 +42,15 @@ import org.apache.hc.core5.http2.frame.F
import org.apache.hc.core5.http2.frame.FrameFlag;
import org.apache.hc.core5.http2.impl.BasicHttp2TransportMetrics;
import org.apache.hc.core5.http2.io.Http2TransportMetrics;
-import org.apache.hc.core5.http2.io.SessionInputBuffer;
import org.apache.hc.core5.util.Args;
/**
+ * Frame input buffer for HTTP/2 blocking connections.
*
* @since 5.0
*/
@NotThreadSafe
-public class SessionInputBufferImpl implements SessionInputBuffer {
+public final class FrameInputBuffer {
private final BasicHttp2TransportMetrics metrics;
private final int maxFramePayloadSize;
@@ -60,24 +59,28 @@ public class SessionInputBufferImpl impl
private int off;
private int dataLen;
- SessionInputBufferImpl(final BasicHttp2TransportMetrics metrics, final int bufferLen, final int maxFramePayloadSize) {
+ FrameInputBuffer(final BasicHttp2TransportMetrics metrics, final int bufferLen, final int maxFramePayloadSize) {
Args.notNull(metrics, "HTTP2 transport metrcis");
- Args.positive(maxFramePayloadSize, "Buffer size");
+ Args.positive(maxFramePayloadSize, "Maximum payload size");
this.metrics = metrics;
this.maxFramePayloadSize = maxFramePayloadSize;
this.buffer = new byte[bufferLen];
this.dataLen = 0;
}
- public SessionInputBufferImpl(final BasicHttp2TransportMetrics metrics, final int maxFramePayloadSize) {
- this(metrics, FrameConsts.HEAD_LEN + maxFramePayloadSize + FrameConsts.MAX_PADDING + 1, maxFramePayloadSize);
+ public FrameInputBuffer(final BasicHttp2TransportMetrics metrics, final int maxFramePayloadSize) {
+ this(metrics, FrameConsts.HEAD_LEN + maxFramePayloadSize, maxFramePayloadSize);
}
- public SessionInputBufferImpl(final int maxFramePayloadSize) {
+ public FrameInputBuffer(final int maxFramePayloadSize) {
this(new BasicHttp2TransportMetrics(), maxFramePayloadSize);
}
- private void fillBuffer(final InputStream instream, final int requiredLen)throws IOException {
+ boolean hasData() {
+ return this.dataLen > 0;
+ }
+
+ void fillBuffer(final InputStream instream, final int requiredLen) throws IOException {
while (dataLen < requiredLen) {
if (off > 0) {
System.arraycopy(buffer, off, buffer, 0, dataLen);
@@ -97,39 +100,42 @@ public class SessionInputBufferImpl impl
}
}
- public Frame<ByteBuffer> readFrame(final InputStream instream) throws ProtocolException, IOException {
+ public Frame<ByteBuffer> read(final InputStream instream) throws IOException {
fillBuffer(instream, FrameConsts.HEAD_LEN);
- int payloadOff = FrameConsts.HEAD_LEN;
+ final int payloadOff = FrameConsts.HEAD_LEN;
final int payloadLen = (buffer[off] & 0xff) << 16 | (buffer[off + 1] & 0xff) << 8 | (buffer[off + 2] & 0xff);
final int type = buffer[off + 3] & 0xff;
final int flags = buffer[off + 4] & 0xff;
- final int streamId = (buffer[off + 5] & 0xff) << 24 | (buffer[off + 6] & 0xff << 16) | (buffer[off + 7] & 0xff) << 8 | (buffer[off + 8] & 0xff);
+ final int streamId = Math.abs(buffer[off + 5] & 0xff) << 24 | (buffer[off + 6] & 0xff << 16) | (buffer[off + 7] & 0xff) << 8 | (buffer[off + 8] & 0xff);
if (payloadLen > maxFramePayloadSize) {
- throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, streamId, "Frame size exceeds maximum");
- }
-
- final int padding;
- if ((flags & FrameFlag.PADDED.getValue()) == 0) {
- padding = 0;
- } else {
- fillBuffer(instream, FrameConsts.HEAD_LEN + 1);
- payloadOff = FrameConsts.HEAD_LEN + 1;
- padding = buffer[off + 9] & 0xff;
+ throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, "Frame size exceeds maximum");
}
- final int frameLen = payloadOff + payloadLen + padding;
+ final int frameLen = payloadOff + payloadLen;
fillBuffer(instream, frameLen);
final ByteBuffer payload;
if (payloadLen > 0) {
- payload = ByteBuffer.wrap(buffer, off + payloadOff, payloadLen).asReadOnlyBuffer();
+ if ((flags & FrameFlag.PADDED.getValue()) == 0) {
+ payload = ByteBuffer.wrap(buffer, off + payloadOff, payloadLen);
+ } else {
+ if (payloadLen == 0) {
+ throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Inconsistent padding");
+ }
+ final int padding = buffer[off + FrameConsts.HEAD_LEN] & 0xff;
+ if (payloadLen < padding + 1) {
+ throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Inconsistent padding");
+ }
+ payload = ByteBuffer.wrap(buffer, off + payloadOff + 1, payloadLen - padding - 1);
+ }
} else {
payload = null;
}
- final ByteBufferFrame frame = new ByteBufferFrame(type, flags, streamId, payload);
+ final ByteBufferFrame frame = new ByteBufferFrame(type, flags & ~FrameFlag.PADDED.getValue(), streamId,
+ payload != null ? payload.asReadOnlyBuffer() : null);
off += frameLen;
dataLen -= frameLen;
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameInputBuffer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputBuffer.java (from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputBuffer.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputBuffer.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java&r1=1742947&r2=1742948&rev=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputBuffer.java Mon May 9 13:31:45 2016
@@ -32,7 +32,6 @@ import java.io.OutputStream;
import java.nio.ByteBuffer;
import org.apache.hc.core5.annotation.NotThreadSafe;
-import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http2.H2ConnectionException;
import org.apache.hc.core5.http2.H2Error;
import org.apache.hc.core5.http2.frame.Frame;
@@ -40,35 +39,34 @@ import org.apache.hc.core5.http2.frame.F
import org.apache.hc.core5.http2.frame.FrameFlag;
import org.apache.hc.core5.http2.impl.BasicHttp2TransportMetrics;
import org.apache.hc.core5.http2.io.Http2TransportMetrics;
-import org.apache.hc.core5.http2.io.SessionOutputBuffer;
import org.apache.hc.core5.util.Args;
/**
- * Session output buffer for HTTP/2 blocking connections.
+ * Frame output buffer for HTTP/2 blocking connections.
*
* @since 5.0
*/
@NotThreadSafe
-public class SessionOutputBufferImpl implements SessionOutputBuffer {
+public final class FrameOutputBuffer {
private final BasicHttp2TransportMetrics metrics;
private final int maxFramePayloadSize;
private final byte[] buffer;
- public SessionOutputBufferImpl(final BasicHttp2TransportMetrics metrics, final int maxFramePayloadSize) {
+ public FrameOutputBuffer(final BasicHttp2TransportMetrics metrics, final int maxFramePayloadSize) {
super();
Args.notNull(metrics, "HTTP2 transport metrcis");
- Args.positive(maxFramePayloadSize, "Buffer size");
+ Args.positive(maxFramePayloadSize, "Maximum payload size");
this.metrics = metrics;
this.maxFramePayloadSize = maxFramePayloadSize;
this.buffer = new byte[FrameConsts.HEAD_LEN + maxFramePayloadSize + FrameConsts.MAX_PADDING + 1];
}
- public SessionOutputBufferImpl(final int maxFramePayloadSize) {
+ public FrameOutputBuffer(final int maxFramePayloadSize) {
this(new BasicHttp2TransportMetrics(), maxFramePayloadSize);
}
- public void write(final Frame<ByteBuffer> frame, final OutputStream outstream) throws ProtocolException, IOException {
+ public void write(final Frame<ByteBuffer> frame, final OutputStream outstream) throws IOException {
if (frame == null) {
return;
}
@@ -78,7 +76,7 @@ public class SessionOutputBufferImpl imp
final ByteBuffer payload = frame.getPayload();
final int payloadLen = payload != null ? payload.remaining() : 0;
if (payload != null && payload.remaining() > maxFramePayloadSize) {
- throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, streamId, "Frame size exceeds maximum");
+ throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, "Frame size exceeds maximum");
}
buffer[0] = (byte) (payloadLen >> 16 & 0xff);
buffer[1] = (byte) (payloadLen >> 8 & 0xff);
Added: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java?rev=1742948&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java Mon May 9 13:31:45 2016
@@ -0,0 +1,105 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http2.impl.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+import org.apache.hc.core5.annotation.NotThreadSafe;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * @since 5.0
+ */
+@NotThreadSafe
+abstract class FrameOutputStream extends OutputStream {
+
+ private final OutputStream outputStream;
+
+ private final byte[] cache;
+ private int cachePosition = 0;
+
+ public FrameOutputStream(final int minChunkSize, final OutputStream outputStream) {
+ super();
+ this.outputStream = Args.notNull(outputStream, "Output stream");
+ this.cache = new byte[minChunkSize];
+ }
+
+ protected abstract void write(final ByteBuffer src, final boolean endStream, final OutputStream outputStream) throws IOException;
+
+ private void flushCache(final boolean endStream) throws IOException {
+ if (this.cachePosition > 0) {
+ write(ByteBuffer.wrap(this.cache, 0, this.cachePosition), endStream, this.outputStream);
+ this.cachePosition = 0;
+ }
+ }
+
+ @Override
+ public void write(final int b) throws IOException {
+ this.cache[this.cachePosition] = (byte) b;
+ this.cachePosition++;
+ if (this.cachePosition == this.cache.length) {
+ flushCache(false);
+ }
+ }
+
+ @Override
+ public void write(final byte[] b) throws IOException {
+ write(b, 0, b.length);
+ }
+
+ @Override
+ public void write(final byte[] src, final int off, final int len) throws IOException {
+ if (len >= this.cache.length - this.cachePosition) {
+ flushCache(false);
+ write(ByteBuffer.wrap(src, off, len), false, this.outputStream);
+ } else {
+ System.arraycopy(src, off, cache, this.cachePosition, len);
+ this.cachePosition += len;
+ }
+ }
+
+ @Override
+ public void flush() throws IOException {
+ flushCache(false);
+ this.outputStream.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (this.cachePosition > 0) {
+ flushCache(true);
+ } else {
+ write(null, true, this.outputStream);
+ }
+ flushCache(true);
+ this.outputStream.flush();
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/FrameOutputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java?rev=1742948&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java Mon May 9 13:31:45 2016
@@ -0,0 +1,164 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http2.impl.nio;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ReadableByteChannel;
+
+import org.apache.hc.core5.annotation.NotThreadSafe;
+import org.apache.hc.core5.http.ConnectionClosedException;
+import org.apache.hc.core5.http2.H2ConnectionException;
+import org.apache.hc.core5.http2.H2CorruptFrameException;
+import org.apache.hc.core5.http2.H2Error;
+import org.apache.hc.core5.http2.frame.ByteBufferFrame;
+import org.apache.hc.core5.http2.frame.Frame;
+import org.apache.hc.core5.http2.frame.FrameConsts;
+import org.apache.hc.core5.http2.frame.FrameFlag;
+import org.apache.hc.core5.http2.impl.BasicHttp2TransportMetrics;
+import org.apache.hc.core5.http2.io.Http2TransportMetrics;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * Frame input buffer for HTTP/2 non-blocking connections.
+ *
+ * @since 5.0
+ */
+@NotThreadSafe
+public final class FrameInputBuffer {
+
+ enum State { HEAD_EXPECTED, PAYLOAD_EXPECTED }
+
+ private final BasicHttp2TransportMetrics metrics;
+ private final int maxFramePayloadSize;
+ private final byte[] bytes;
+ private final ByteBuffer buffer;
+
+ private State state;
+ private int payloadLen;
+ private int type;
+ private int flags;
+ private int streamId;
+
+ FrameInputBuffer(final BasicHttp2TransportMetrics metrics, final int bufferLen, final int maxFramePayloadSize) {
+ Args.notNull(metrics, "HTTP2 transport metrcis");
+ Args.positive(maxFramePayloadSize, "Maximum payload size");
+ this.metrics = metrics;
+ this.maxFramePayloadSize = maxFramePayloadSize;
+ this.bytes = new byte[bufferLen];
+ this.buffer = ByteBuffer.wrap(bytes);
+ this.buffer.flip();
+ this.state = State.HEAD_EXPECTED;
+ }
+
+ public FrameInputBuffer(final BasicHttp2TransportMetrics metrics, final int maxFramePayloadSize) {
+ this(metrics, FrameConsts.HEAD_LEN + maxFramePayloadSize, maxFramePayloadSize);
+ }
+
+ public FrameInputBuffer(final int maxFramePayloadSize) {
+ this(new BasicHttp2TransportMetrics(), maxFramePayloadSize);
+ }
+
+ public Frame<ByteBuffer> read(final ReadableByteChannel channel) throws IOException {
+ for (;;) {
+ switch (state) {
+ case HEAD_EXPECTED:
+ if (buffer.remaining() >= FrameConsts.HEAD_LEN) {
+ final int lengthAndType = buffer.getInt();
+ payloadLen = lengthAndType >> 8;
+ if (payloadLen > maxFramePayloadSize) {
+ throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, "Frame size exceeds maximum");
+ }
+ type = lengthAndType & 0xff;
+ flags = buffer.get();
+ streamId = Math.abs(buffer.getInt());
+ state = State.PAYLOAD_EXPECTED;
+ } else {
+ break;
+ }
+ case PAYLOAD_EXPECTED:
+ if (buffer.remaining() >= payloadLen) {
+ final ByteBuffer payload;
+ if ((flags & FrameFlag.PADDED.getValue()) == 0) {
+ if (payloadLen > 0) {
+ payload = ByteBuffer.wrap(bytes, buffer.position(), payloadLen);
+ buffer.position(buffer.position() + payloadLen);
+ } else {
+ payload = null;
+ }
+ } else {
+ if (payloadLen == 0) {
+ throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Inconsistent padding");
+ }
+ final int padding = buffer.get();
+ if (payloadLen < padding + 1) {
+ throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Inconsistent padding");
+ }
+ final int off = buffer.position();
+ final int len = payloadLen - padding - 1;
+ payload = ByteBuffer.wrap(bytes, off, len);
+ buffer.position(buffer.position() - 1 + payloadLen);
+ }
+ state = State.HEAD_EXPECTED;
+ metrics.incrementFramesTransferred();
+ return new ByteBufferFrame(type, flags & ~FrameFlag.PADDED.getValue(), streamId,
+ payload != null ? payload.asReadOnlyBuffer() : null);
+ }
+ }
+ if (buffer.hasRemaining()) {
+ buffer.compact();
+ } else {
+ buffer.clear();
+ }
+ final int bytesRead = channel.read(buffer);
+ buffer.flip();
+ if (bytesRead > 0) {
+ metrics.incrementBytesTransferred(bytesRead);
+ }
+ if (bytesRead == 0) {
+ break;
+ } else if (bytesRead < 0) {
+ if (state != State.HEAD_EXPECTED || buffer.hasRemaining()) {
+ throw new H2CorruptFrameException("Corrupt or incomplete HTTP2 frame");
+ } else {
+ throw new ConnectionClosedException("Connection closed");
+ }
+ }
+ }
+ return null;
+ }
+
+ public void reset() {
+ buffer.compact();
+ state = State.HEAD_EXPECTED;
+ }
+
+ public Http2TransportMetrics getMetrics() {
+ return metrics;
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameInputBuffer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java (from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java&r1=1742947&r2=1742948&rev=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/io/SessionOutputBufferImpl.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java Mon May 9 13:31:45 2016
@@ -24,92 +24,97 @@
* <http://www.apache.org/>.
*
*/
-
-package org.apache.hc.core5.http2.impl.io;
+package org.apache.hc.core5.http2.impl.nio;
import java.io.IOException;
-import java.io.OutputStream;
import java.nio.ByteBuffer;
+import java.nio.channels.GatheringByteChannel;
+import java.nio.channels.WritableByteChannel;
import org.apache.hc.core5.annotation.NotThreadSafe;
-import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http2.H2ConnectionException;
import org.apache.hc.core5.http2.H2Error;
import org.apache.hc.core5.http2.frame.Frame;
import org.apache.hc.core5.http2.frame.FrameConsts;
-import org.apache.hc.core5.http2.frame.FrameFlag;
import org.apache.hc.core5.http2.impl.BasicHttp2TransportMetrics;
import org.apache.hc.core5.http2.io.Http2TransportMetrics;
-import org.apache.hc.core5.http2.io.SessionOutputBuffer;
import org.apache.hc.core5.util.Args;
/**
- * Session output buffer for HTTP/2 blocking connections.
+ * Frame output buffer for HTTP/2 non-blocking connections.
*
* @since 5.0
*/
@NotThreadSafe
-public class SessionOutputBufferImpl implements SessionOutputBuffer {
+public final class FrameOutputBuffer {
private final BasicHttp2TransportMetrics metrics;
private final int maxFramePayloadSize;
- private final byte[] buffer;
+ private final ByteBuffer buffer;
- public SessionOutputBufferImpl(final BasicHttp2TransportMetrics metrics, final int maxFramePayloadSize) {
- super();
+ public FrameOutputBuffer(final BasicHttp2TransportMetrics metrics, final int maxFramePayloadSize) {
Args.notNull(metrics, "HTTP2 transport metrcis");
- Args.positive(maxFramePayloadSize, "Buffer size");
+ Args.positive(maxFramePayloadSize, "Maximum payload size");
this.metrics = metrics;
this.maxFramePayloadSize = maxFramePayloadSize;
- this.buffer = new byte[FrameConsts.HEAD_LEN + maxFramePayloadSize + FrameConsts.MAX_PADDING + 1];
+ this.buffer = ByteBuffer.allocate(FrameConsts.HEAD_LEN + maxFramePayloadSize);
}
- public SessionOutputBufferImpl(final int maxFramePayloadSize) {
+ public FrameOutputBuffer(final int maxFramePayloadSize) {
this(new BasicHttp2TransportMetrics(), maxFramePayloadSize);
}
- public void write(final Frame<ByteBuffer> frame, final OutputStream outstream) throws ProtocolException, IOException {
- if (frame == null) {
- return;
+ private void writeToChannel(final WritableByteChannel channel, final ByteBuffer src) throws IOException {
+ final int bytesWritten = channel.write(src);
+ if (bytesWritten > 0) {
+ metrics.incrementBytesTransferred(bytesWritten);
}
- final int type = frame.getType();
- final long streamId = frame.getStreamId();
- final int flags = frame.getFlags();
+ }
+
+ public void write(final Frame<ByteBuffer> frame, final WritableByteChannel channel) throws IOException {
+ Args.notNull(frame, "Frame");
+
final ByteBuffer payload = frame.getPayload();
- final int payloadLen = payload != null ? payload.remaining() : 0;
if (payload != null && payload.remaining() > maxFramePayloadSize) {
- throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, streamId, "Frame size exceeds maximum");
- }
- buffer[0] = (byte) (payloadLen >> 16 & 0xff);
- buffer[1] = (byte) (payloadLen >> 8 & 0xff);
- buffer[2] = (byte) (payloadLen & 0xff);
-
- buffer[3] = (byte) (type & 0xff);
- buffer[4] = (byte) (flags & 0xff);
-
- buffer[5] = (byte) (streamId >> 24 & 0xff);
- buffer[6] = (byte) (streamId >> 16 & 0xff);
- buffer[7] = (byte) (streamId >> 8 & 0xff);;
- buffer[8] = (byte) (streamId & 0xff);
-
- int frameLen = FrameConsts.HEAD_LEN;
- int padding = 0;
- if ((flags & FrameFlag.PADDED.getValue()) > 0) {
- padding = 16;
- buffer[9] = (byte) (padding & 0xff);
- frameLen++;
+ throw new H2ConnectionException(H2Error.FRAME_SIZE_ERROR, "Frame size exceeds maximum");
}
+
+ buffer.putInt((payload != null ? payload.remaining() << 8 : 0) | (frame.getType() & 0xff));
+ buffer.put((byte) (frame.getFlags() & 0xff));
+ buffer.putInt(frame.getStreamId());
+
if (payload != null) {
- payload.get(buffer, frameLen, payload.remaining());
- frameLen += payloadLen;
+ if (channel instanceof GatheringByteChannel) {
+ buffer.flip();
+ ((GatheringByteChannel) channel).write(new ByteBuffer[]{buffer, payload});
+ buffer.compact();
+ if (payload.hasRemaining()) {
+ buffer.put(payload);
+ }
+ } else {
+ buffer.put(payload);
+ }
}
- for (int i = 0; i < padding; i++) {
- buffer[frameLen++] = 0;
+
+ if (buffer.position() > 0) {
+ buffer.flip();
+ writeToChannel(channel, buffer);
+ buffer.compact();
}
- outstream.write(buffer, 0, frameLen);
metrics.incrementFramesTransferred();
- metrics.incrementBytesTransferred(frameLen);
+ }
+
+ public void flush(final WritableByteChannel channel) throws IOException {
+ if (buffer.position() > 0) {
+ buffer.flip();
+ writeToChannel(channel, buffer);
+ buffer.compact();
+ }
+ }
+
+ public boolean isEmpty() {
+ return buffer.position() == 0;
}
public Http2TransportMetrics getMetrics() {
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/FrameOutputBuffer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestFrameInOutBuffers.java (from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestFrameInOutBuffers.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestFrameInOutBuffers.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java&r1=1742947&r2=1742948&rev=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestFrameInOutBuffers.java Mon May 9 13:31:45 2016
@@ -40,23 +40,21 @@ import org.apache.hc.core5.http2.frame.F
import org.apache.hc.core5.http2.frame.FrameFlag;
import org.apache.hc.core5.http2.frame.FrameType;
import org.apache.hc.core5.http2.impl.BasicHttp2TransportMetrics;
-import org.apache.hc.core5.http2.io.SessionInputBuffer;
-import org.apache.hc.core5.http2.io.SessionOutputBuffer;
import org.junit.Assert;
import org.junit.Test;
-public class TestSessionInOutBuffers {
+public class TestFrameInOutBuffers {
@Test
public void testReadWriteFrame() throws Exception {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16 * 1024);
+ final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
final ByteBufferFrame frame = new ByteBufferFrame(FrameType.DATA.getValue(), 0, 1,
ByteBuffer.wrap(new byte[]{1,2,3,4,5}));
outbuffer.write(frame, outputStream);
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
final byte[] bytes = outputStream.toByteArray();
final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
Assert.assertEquals(FrameConsts.HEAD_LEN + 5, bytes.length);
@@ -64,7 +62,7 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(1, outbuffer.getMetrics().getFramesTransferred());
Assert.assertEquals(bytes.length, outbuffer.getMetrics().getBytesTransferred());
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame2 = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA.getValue(), frame2.getType());
Assert.assertEquals(0, frame2.getFlags());
Assert.assertEquals(1L, frame2.getStreamId());
@@ -83,52 +81,17 @@ public class TestSessionInOutBuffers {
}
@Test
- public void testReadWriteFrameWithPadding() throws Exception {
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16 * 1024);
-
- final ByteBufferFrame frame = new ByteBufferFrame(FrameType.DATA.getValue(), FrameFlag.of(FrameFlag.PADDED), 1,
- ByteBuffer.wrap(new byte[]{1,2,3,4,5}));
- outbuffer.write(frame, outputStream);
-
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final byte[] bytes = outputStream.toByteArray();
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
- Assert.assertTrue(bytes.length > FrameConsts.HEAD_LEN + 1 + 5);
-
- Assert.assertEquals(1, outbuffer.getMetrics().getFramesTransferred());
- Assert.assertEquals(bytes.length, outbuffer.getMetrics().getBytesTransferred());
-
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
- Assert.assertEquals(FrameType.DATA.getValue(), frame2.getType());
- Assert.assertEquals(FrameFlag.PADDED.getValue(), frame2.getFlags());
- Assert.assertEquals(1L, frame2.getStreamId());
- final ByteBuffer payload2 = frame2.getPayload();
- Assert.assertNotNull(payload2);
- Assert.assertEquals(5, payload2.remaining());
- Assert.assertEquals(1, payload2.get());
- Assert.assertEquals(2, payload2.get());
- Assert.assertEquals(3, payload2.get());
- Assert.assertEquals(4, payload2.get());
- Assert.assertEquals(5, payload2.get());
- Assert.assertEquals(-1, inputStream.read());
-
- Assert.assertEquals(1, inbuffer.getMetrics().getFramesTransferred());
- Assert.assertEquals(bytes.length, inbuffer.getMetrics().getBytesTransferred());
- }
-
- @Test
public void testReadFrameMultiple() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(
new byte[] {
- 0,0,5,0,8,0,0,0,8,4,0,1,2,3,4,0,0,0,0,
- 0,0,5,0,9,0,0,0,8,4,5,6,7,8,9,0,0,0,0
+ 0,0,10,0,8,0,0,0,8,4,0,1,2,3,4,0,0,0,0,
+ 0,0,10,0,9,0,0,0,8,4,5,6,7,8,9,0,0,0,0
});
- final Frame<ByteBuffer> frame1 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame1 = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame1.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED), frame1.getFlags());
+ Assert.assertEquals(0, frame1.getFlags());
Assert.assertEquals(8, frame1.getStreamId());
final ByteBuffer payload1 = frame1.getPayload();
Assert.assertNotNull(payload1);
@@ -139,9 +102,9 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(3, payload1.get());
Assert.assertEquals(4, payload1.get());
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame2 = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame2.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED, FrameFlag.END_STREAM), frame2.getFlags());
+ Assert.assertEquals(FrameFlag.of(FrameFlag.END_STREAM), frame2.getFlags());
Assert.assertEquals(8, frame2.getStreamId());
final ByteBuffer payload2 = frame2.getPayload();
Assert.assertNotNull(payload2);
@@ -157,17 +120,17 @@ public class TestSessionInOutBuffers {
@Test
public void testReadFrameMultipleSmallBuffer() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(new BasicHttp2TransportMetrics(), 20, 5);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(new BasicHttp2TransportMetrics(), 20, 10);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(
new byte[] {
- 0,0,5,0,8,0,0,0,8,4,1,1,1,1,1,0,0,0,0,
+ 0,0,10,0,8,0,0,0,8,4,1,1,1,1,1,0,0,0,0,
0,0,5,0,0,0,0,0,8,2,2,2,2,2,
- 0,0,5,0,9,0,0,0,8,4,3,3,3,3,3,0,0,0,0
+ 0,0,10,0,9,0,0,0,8,4,3,3,3,3,3,0,0,0,0
});
- final Frame<ByteBuffer> frame1 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame1 = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame1.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED), frame1.getFlags());
+ Assert.assertEquals(0, frame1.getFlags());
Assert.assertEquals(8, frame1.getStreamId());
final ByteBuffer payload1 = frame1.getPayload();
Assert.assertNotNull(payload1);
@@ -178,7 +141,7 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(1, payload1.get());
Assert.assertEquals(1, payload1.get());
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame2 = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame2.getType()));
Assert.assertEquals(0, frame2.getFlags());
Assert.assertEquals(8, frame2.getStreamId());
@@ -191,9 +154,9 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(2, payload2.get());
Assert.assertEquals(2, payload2.get());
- final Frame<ByteBuffer> frame3 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame3 = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame3.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED, FrameFlag.END_STREAM), frame3.getFlags());
+ Assert.assertEquals(FrameFlag.of(FrameFlag.END_STREAM), frame3.getFlags());
Assert.assertEquals(8, frame3.getStreamId());
final ByteBuffer payload3 = frame3.getPayload();
Assert.assertNotNull(payload3);
@@ -209,19 +172,19 @@ public class TestSessionInOutBuffers {
@Test
public void testReadFramePartialReads() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
final MultiByteArrayInputStream inputStream = new MultiByteArrayInputStream(
new byte[] {0,0},
- new byte[] {5,0,9,0},
+ new byte[] {10,0,9,0},
new byte[] {0,0,8},
new byte[] {4},
new byte[] {1,2,3,4},
new byte[] {5,0},
new byte[] {0,0,0});
- final Frame<ByteBuffer> frame = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED, FrameFlag.END_STREAM), frame.getFlags());
+ Assert.assertEquals(FrameFlag.of(FrameFlag.END_STREAM), frame.getFlags());
Assert.assertEquals(8, frame.getStreamId());
final ByteBuffer payload = frame.getPayload();
Assert.assertNotNull(payload);
@@ -236,10 +199,10 @@ public class TestSessionInOutBuffers {
@Test
public void testReadEmptyFrame() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {0,0,0,0,0,0,0,0,0});
- final Frame<ByteBuffer> frame = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame = inbuffer.read(inputStream);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame.getType()));
Assert.assertEquals(0, frame.getFlags());
Assert.assertEquals(0, frame.getStreamId());
@@ -249,24 +212,24 @@ public class TestSessionInOutBuffers {
@Test(expected = ConnectionClosedException.class)
public void testReadFrameConnectionClosed() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
- inbuffer.readFrame(inputStream);
+ inbuffer.read(inputStream);
}
@Test(expected = H2CorruptFrameException.class)
public void testReadFrameCorruptFrame() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {0,0});
- inbuffer.readFrame(inputStream);
+ inbuffer.read(inputStream);
}
@Test(expected = H2ConnectionException.class)
public void testWriteFrameExceedingLimit() throws Exception {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(1024);
+ final FrameOutputBuffer outbuffer = new FrameOutputBuffer(1024);
final ByteBufferFrame frame = new ByteBufferFrame(FrameType.DATA.getValue(), 0, 1,
ByteBuffer.wrap(new byte[2048]));
@@ -275,11 +238,11 @@ public class TestSessionInOutBuffers {
@Test(expected = H2ConnectionException.class)
public void testReadFrameExceedingLimit() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(
new byte[] {0,-128,-128,0,0,0,0,0,1});
- inbuffer.readFrame(inputStream);
+ inbuffer.read(inputStream);
}
}
Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java (from r1742947, httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java&r1=1742947&r2=1742948&rev=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/TestSessionInOutBuffers.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java Mon May 9 13:31:45 2016
@@ -25,10 +25,8 @@
*
*/
-package org.apache.hc.core5.http2.impl.io;
+package org.apache.hc.core5.http2.impl.nio;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import org.apache.hc.core5.http.ConnectionClosedException;
@@ -40,31 +38,29 @@ import org.apache.hc.core5.http2.frame.F
import org.apache.hc.core5.http2.frame.FrameFlag;
import org.apache.hc.core5.http2.frame.FrameType;
import org.apache.hc.core5.http2.impl.BasicHttp2TransportMetrics;
-import org.apache.hc.core5.http2.io.SessionInputBuffer;
-import org.apache.hc.core5.http2.io.SessionOutputBuffer;
import org.junit.Assert;
import org.junit.Test;
-public class TestSessionInOutBuffers {
+public class TestFrameInOutBuffers {
@Test
public void testReadWriteFrame() throws Exception {
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16 * 1024);
+ final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
+ final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
final ByteBufferFrame frame = new ByteBufferFrame(FrameType.DATA.getValue(), 0, 1,
ByteBuffer.wrap(new byte[]{1,2,3,4,5}));
- outbuffer.write(frame, outputStream);
+ outbuffer.write(frame, writableChannel);
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final byte[] bytes = outputStream.toByteArray();
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
+ final byte[] bytes = writableChannel.toByteArray();
Assert.assertEquals(FrameConsts.HEAD_LEN + 5, bytes.length);
Assert.assertEquals(1, outbuffer.getMetrics().getFramesTransferred());
Assert.assertEquals(bytes.length, outbuffer.getMetrics().getBytesTransferred());
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(bytes);
+ final Frame<ByteBuffer> frame2 = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA.getValue(), frame2.getType());
Assert.assertEquals(0, frame2.getFlags());
Assert.assertEquals(1L, frame2.getStreamId());
@@ -76,59 +72,46 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(3, payload2.get());
Assert.assertEquals(4, payload2.get());
Assert.assertEquals(5, payload2.get());
- Assert.assertEquals(-1, inputStream.read());
+ Assert.assertEquals(-1, readableChannel.read(ByteBuffer.allocate(1024)));
Assert.assertEquals(1, inbuffer.getMetrics().getFramesTransferred());
Assert.assertEquals(bytes.length, inbuffer.getMetrics().getBytesTransferred());
}
@Test
- public void testReadWriteFrameWithPadding() throws Exception {
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16 * 1024);
-
- final ByteBufferFrame frame = new ByteBufferFrame(FrameType.DATA.getValue(), FrameFlag.of(FrameFlag.PADDED), 1,
- ByteBuffer.wrap(new byte[]{1,2,3,4,5}));
- outbuffer.write(frame, outputStream);
-
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final byte[] bytes = outputStream.toByteArray();
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
- Assert.assertTrue(bytes.length > FrameConsts.HEAD_LEN + 1 + 5);
-
- Assert.assertEquals(1, outbuffer.getMetrics().getFramesTransferred());
- Assert.assertEquals(bytes.length, outbuffer.getMetrics().getBytesTransferred());
-
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
- Assert.assertEquals(FrameType.DATA.getValue(), frame2.getType());
- Assert.assertEquals(FrameFlag.PADDED.getValue(), frame2.getFlags());
- Assert.assertEquals(1L, frame2.getStreamId());
- final ByteBuffer payload2 = frame2.getPayload();
- Assert.assertNotNull(payload2);
- Assert.assertEquals(5, payload2.remaining());
- Assert.assertEquals(1, payload2.get());
- Assert.assertEquals(2, payload2.get());
- Assert.assertEquals(3, payload2.get());
- Assert.assertEquals(4, payload2.get());
- Assert.assertEquals(5, payload2.get());
- Assert.assertEquals(-1, inputStream.read());
-
- Assert.assertEquals(1, inbuffer.getMetrics().getFramesTransferred());
- Assert.assertEquals(bytes.length, inbuffer.getMetrics().getBytesTransferred());
+ public void testPartialFrameWrite() throws Exception {
+ final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024, FrameConsts.HEAD_LEN + 10);
+ final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
+ final ByteBufferFrame frame = new ByteBufferFrame(FrameType.DATA.getValue(), FrameFlag.END_STREAM.getValue(), 5,
+ ByteBuffer.wrap(new byte[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}));
+
+ outbuffer.write(frame, writableChannel);
+ Assert.assertArrayEquals(new byte[] {0,0,16,0,1,0,0,0,5,48,49,50,51,52,53,54,55,56,57},
+ writableChannel.toByteArray());
+
+ Assert.assertFalse(outbuffer.isEmpty());
+ outbuffer.flush(writableChannel);
+ Assert.assertArrayEquals(new byte[] {0,0,16,0,1,0,0,0,5,48,49,50,51,52,53,54,55,56,57},
+ writableChannel.toByteArray());
+
+ writableChannel.flush();
+ outbuffer.flush(writableChannel);
+ Assert.assertArrayEquals(new byte[] {0,0,16,0,1,0,0,0,5,48,49,50,51,52,53,54,55,56,57,97,98,99,100,101,102},
+ writableChannel.toByteArray());
}
@Test
public void testReadFrameMultiple() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(
new byte[] {
- 0,0,5,0,8,0,0,0,8,4,0,1,2,3,4,0,0,0,0,
- 0,0,5,0,9,0,0,0,8,4,5,6,7,8,9,0,0,0,0
+ 0,0,10,0,8,0,0,0,8,4,0,1,2,3,4,0,0,0,0,
+ 0,0,10,0,9,0,0,0,8,4,5,6,7,8,9,0,0,0,0
});
- final Frame<ByteBuffer> frame1 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame1 = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame1.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED), frame1.getFlags());
+ Assert.assertEquals(0, frame1.getFlags());
Assert.assertEquals(8, frame1.getStreamId());
final ByteBuffer payload1 = frame1.getPayload();
Assert.assertNotNull(payload1);
@@ -139,9 +122,9 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(3, payload1.get());
Assert.assertEquals(4, payload1.get());
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame2 = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame2.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED, FrameFlag.END_STREAM), frame2.getFlags());
+ Assert.assertEquals(FrameFlag.of(FrameFlag.END_STREAM), frame2.getFlags());
Assert.assertEquals(8, frame2.getStreamId());
final ByteBuffer payload2 = frame2.getPayload();
Assert.assertNotNull(payload2);
@@ -152,22 +135,22 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(8, payload2.get());
Assert.assertEquals(9, payload2.get());
- Assert.assertEquals(-1, inputStream.read());
+ Assert.assertEquals(-1, readableChannel.read(ByteBuffer.allocate(1024)));
}
@Test
public void testReadFrameMultipleSmallBuffer() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(new BasicHttp2TransportMetrics(), 20, 5);
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(new BasicHttp2TransportMetrics(), 20, 10);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(
new byte[] {
- 0,0,5,0,8,0,0,0,8,4,1,1,1,1,1,0,0,0,0,
+ 0,0,10,0,8,0,0,0,8,4,1,1,1,1,1,0,0,0,0,
0,0,5,0,0,0,0,0,8,2,2,2,2,2,
- 0,0,5,0,9,0,0,0,8,4,3,3,3,3,3,0,0,0,0
+ 0,0,10,0,9,0,0,0,8,4,3,3,3,3,3,0,0,0,0
});
- final Frame<ByteBuffer> frame1 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame1 = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame1.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED), frame1.getFlags());
+ Assert.assertEquals(0, frame1.getFlags());
Assert.assertEquals(8, frame1.getStreamId());
final ByteBuffer payload1 = frame1.getPayload();
Assert.assertNotNull(payload1);
@@ -178,7 +161,7 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(1, payload1.get());
Assert.assertEquals(1, payload1.get());
- final Frame<ByteBuffer> frame2 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame2 = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame2.getType()));
Assert.assertEquals(0, frame2.getFlags());
Assert.assertEquals(8, frame2.getStreamId());
@@ -191,9 +174,9 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(2, payload2.get());
Assert.assertEquals(2, payload2.get());
- final Frame<ByteBuffer> frame3 = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame3 = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame3.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED, FrameFlag.END_STREAM), frame3.getFlags());
+ Assert.assertEquals(FrameFlag.of(FrameFlag.END_STREAM), frame3.getFlags());
Assert.assertEquals(8, frame3.getStreamId());
final ByteBuffer payload3 = frame3.getPayload();
Assert.assertNotNull(payload3);
@@ -204,24 +187,24 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(3, payload3.get());
Assert.assertEquals(3, payload3.get());
- Assert.assertEquals(-1, inputStream.read());
+ Assert.assertEquals(-1, readableChannel.read(ByteBuffer.allocate(1024)));
}
@Test
public void testReadFramePartialReads() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final MultiByteArrayInputStream inputStream = new MultiByteArrayInputStream(
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(
new byte[] {0,0},
- new byte[] {5,0,9,0},
+ new byte[] {10,0,9,0},
new byte[] {0,0,8},
new byte[] {4},
new byte[] {1,2,3,4},
new byte[] {5,0},
new byte[] {0,0,0});
- final Frame<ByteBuffer> frame = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame.getType()));
- Assert.assertEquals(FrameFlag.of(FrameFlag.PADDED, FrameFlag.END_STREAM), frame.getFlags());
+ Assert.assertEquals(FrameFlag.of(FrameFlag.END_STREAM), frame.getFlags());
Assert.assertEquals(8, frame.getStreamId());
final ByteBuffer payload = frame.getPayload();
Assert.assertNotNull(payload);
@@ -231,15 +214,17 @@ public class TestSessionInOutBuffers {
Assert.assertEquals(3, payload.get());
Assert.assertEquals(4, payload.get());
Assert.assertEquals(5, payload.get());
- Assert.assertEquals(-1, inputStream.read());
+
+ Assert.assertEquals(-1, readableChannel.read(ByteBuffer.allocate(1024)));
}
@Test
public void testReadEmptyFrame() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {0,0,0,0,0,0,0,0,0});
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(
+ new byte[] {0,0,0,0,0,0,0,0,0});
- final Frame<ByteBuffer> frame = inbuffer.readFrame(inputStream);
+ final Frame<ByteBuffer> frame = inbuffer.read(readableChannel);
Assert.assertEquals(FrameType.DATA, FrameType.valueOf(frame.getType()));
Assert.assertEquals(0, frame.getFlags());
Assert.assertEquals(0, frame.getStreamId());
@@ -249,37 +234,40 @@ public class TestSessionInOutBuffers {
@Test(expected = ConnectionClosedException.class)
public void testReadFrameConnectionClosed() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(new byte[] {});
- inbuffer.readFrame(inputStream);
+ Assert.assertEquals(null, inbuffer.read(readableChannel));
+ inbuffer.read(readableChannel);
}
@Test(expected = H2CorruptFrameException.class)
public void testReadFrameCorruptFrame() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {0,0});
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(new byte[] {0,0});
- inbuffer.readFrame(inputStream);
+ Assert.assertEquals(null, inbuffer.read(readableChannel));
+ inbuffer.read(readableChannel);
}
@Test(expected = H2ConnectionException.class)
public void testWriteFrameExceedingLimit() throws Exception {
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(1024);
+ final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
+ final FrameOutputBuffer outbuffer = new FrameOutputBuffer(1024);
final ByteBufferFrame frame = new ByteBufferFrame(FrameType.DATA.getValue(), 0, 1,
ByteBuffer.wrap(new byte[2048]));
- outbuffer.write(frame, outputStream);
+ outbuffer.write(frame, writableChannel);
}
@Test(expected = H2ConnectionException.class)
public void testReadFrameExceedingLimit() throws Exception {
- final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16 * 1024);
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(
+ final FrameInputBuffer inbuffer = new FrameInputBuffer(16 * 1024);
+ final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(
new byte[] {0,-128,-128,0,0,0,0,0,1});
- inbuffer.readFrame(inputStream);
+ Assert.assertEquals(null, inbuffer.read(readableChannel));
+ inbuffer.read(readableChannel);
}
}
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/TestFrameInOutBuffers.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java?rev=1742948&r1=1742947&r2=1742948&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java Mon May 9 13:31:45 2016
@@ -108,8 +108,16 @@ public class WritableByteChannelMock imp
public ByteBuffer asReadOnly() {
final ByteBuffer dup = this.buf.duplicate();
- dup.reset();
+ dup.flip();
return dup.asReadOnlyBuffer();
}
+ public byte[] toByteArray() {
+ final ByteBuffer dup = this.buf.duplicate();
+ dup.flip();
+ final byte[] bytes = new byte[dup.remaining()];
+ dup.get(bytes);
+ return bytes;
+ }
+
}
\ No newline at end of file