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 volatile int maxFramePayloadSize;
49      private volatile 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      public void expand(final int maxFramePayloadSize) {
64          this.maxFramePayloadSize = maxFramePayloadSize;
65          final ByteBuffer newBuffer = ByteBuffer.allocate(FrameConsts.HEAD_LEN + maxFramePayloadSize);
66          if (buffer.position() > 0) {
67              buffer.flip();
68              newBuffer.put(buffer);
69          }
70          buffer = newBuffer;
71      }
72  
73      public void write(final RawFrame frame, final WritableByteChannel channel) throws IOException {
74          Args.notNull(frame, "Frame");
75  
76          final ByteBuffer payload = frame.getPayload();
77          Args.check(payload == null || payload.remaining() <= maxFramePayloadSize, "Frame size exceeds maximum");
78          buffer.putInt((payload != null ? payload.remaining() << 8 : 0) | (frame.getType() & 0xff));
79          buffer.put((byte) (frame.getFlags() & 0xff));
80          buffer.putInt(frame.getStreamId());
81  
82          if (payload != null) {
83              if (channel instanceof GatheringByteChannel) {
84                  buffer.flip();
85                  ((GatheringByteChannel) channel).write(new ByteBuffer[]{buffer, payload});
86                  buffer.compact();
87                  if (payload.hasRemaining()) {
88                      buffer.put(payload);
89                  }
90              } else {
91                  buffer.put(payload);
92              }
93          }
94  
95          flush(channel);
96  
97          metrics.incrementFramesTransferred();
98      }
99  
100     public void flush(final WritableByteChannel channel) throws IOException {
101         if (buffer.position() > 0) {
102             buffer.flip();
103             try {
104                 final int bytesWritten = channel.write(buffer);
105                 if (bytesWritten > 0) {
106                     metrics.incrementBytesTransferred(bytesWritten);
107                 }
108             } finally {
109                 buffer.compact();
110             }
111         }
112     }
113 
114     public boolean isEmpty() {
115         return buffer.position() == 0;
116     }
117 
118     public H2TransportMetrics getMetrics() {
119         return metrics;
120     }
121 
122 }