View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.core5.http2.impl.nio;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.nio.channels.GatheringByteChannel;
32  import java.nio.channels.WritableByteChannel;
33  
34  import org.apache.hc.core5.http2.H2TransportMetrics;
35  import org.apache.hc.core5.http2.frame.FrameConsts;
36  import org.apache.hc.core5.http2.frame.RawFrame;
37  import org.apache.hc.core5.http2.impl.BasicH2TransportMetrics;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * Frame output buffer for HTTP/2 non-blocking connections.
42   *
43   * @since 5.0
44   */
45  public final class FrameOutputBuffer {
46  
47      private final BasicH2TransportMetrics metrics;
48      private int maxFramePayloadSize;
49      private ByteBuffer buffer;
50  
51      public FrameOutputBuffer(final BasicH2TransportMetrics metrics, final int maxFramePayloadSize) {
52          Args.notNull(metrics, "HTTP2 transport metrics");
53          Args.positive(maxFramePayloadSize, "Maximum payload size");
54          this.metrics = metrics;
55          this.maxFramePayloadSize = maxFramePayloadSize;
56          this.buffer = ByteBuffer.allocate(FrameConsts.HEAD_LEN + maxFramePayloadSize);
57      }
58  
59      public FrameOutputBuffer(final int maxFramePayloadSize) {
60          this(new BasicH2TransportMetrics(), maxFramePayloadSize);
61      }
62  
63      /**
64       * @deprecated Misnomer. Use {@link #resize(int)}.
65       */
66      @Deprecated
67      public void expand(final int maxFramePayloadSize) {
68          resize(maxFramePayloadSize);
69      }
70  
71      /**
72       * @since 5.2
73       */
74      public int getMaxFramePayloadSize() {
75          return maxFramePayloadSize;
76      }
77  
78      /**
79       * @since 5.2
80       */
81      public void resize(final int maxFramePayloadSize) {
82          if (buffer.capacity() == maxFramePayloadSize) {
83              return;
84          }
85          this.maxFramePayloadSize = maxFramePayloadSize;
86          final ByteBuffer newBuffer = ByteBuffer.allocate(FrameConsts.HEAD_LEN + maxFramePayloadSize);
87          if (buffer.position() > 0) {
88              buffer.flip();
89              newBuffer.put(buffer);
90          }
91          buffer = newBuffer;
92      }
93  
94      public void write(final RawFrame frame, final WritableByteChannel channel) throws IOException {
95          Args.notNull(frame, "Frame");
96  
97          final ByteBuffer payload = frame.getPayload();
98          Args.check(payload == null || payload.remaining() <= maxFramePayloadSize, "Frame size exceeds maximum");
99          buffer.putInt((payload != null ? payload.remaining() << 8 : 0) | (frame.getType() & 0xff));
100         buffer.put((byte) (frame.getFlags() & 0xff));
101         buffer.putInt(frame.getStreamId());
102 
103         if (payload != null) {
104             if (channel instanceof GatheringByteChannel) {
105                 buffer.flip();
106                 ((GatheringByteChannel) channel).write(new ByteBuffer[]{buffer, payload});
107                 buffer.compact();
108                 if (payload.hasRemaining()) {
109                     buffer.put(payload);
110                 }
111             } else {
112                 buffer.put(payload);
113             }
114         }
115 
116         flush(channel);
117 
118         metrics.incrementFramesTransferred();
119     }
120 
121     public void flush(final WritableByteChannel channel) throws IOException {
122         if (buffer.position() > 0) {
123             buffer.flip();
124             try {
125                 final int bytesWritten = channel.write(buffer);
126                 if (bytesWritten > 0) {
127                     metrics.incrementBytesTransferred(bytesWritten);
128                 }
129             } finally {
130                 buffer.compact();
131             }
132         }
133     }
134 
135     public boolean isEmpty() {
136         return buffer.position() == 0;
137     }
138 
139     public H2TransportMetrics getMetrics() {
140         return metrics;
141     }
142 
143 }