View Javadoc

1   /*
2    * Copyright (c) 2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.eclipse.jetty.spdy.generator;
18  
19  import java.nio.ByteBuffer;
20  
21  import org.eclipse.jetty.spdy.ByteBufferPool;
22  import org.eclipse.jetty.spdy.api.DataInfo;
23  import org.eclipse.jetty.spdy.frames.DataFrame;
24  
25  public class DataFrameGenerator
26  {
27      private final ByteBufferPool bufferPool;
28  
29      public DataFrameGenerator(ByteBufferPool bufferPool)
30      {
31          this.bufferPool = bufferPool;
32      }
33  
34      public ByteBuffer generate(int streamId, int length, DataInfo dataInfo)
35      {
36          ByteBuffer buffer = bufferPool.acquire(DataFrame.HEADER_LENGTH + length, true);
37          buffer.position(DataFrame.HEADER_LENGTH);
38          // Guaranteed to always be >= 0
39          int read = dataInfo.readInto(buffer);
40  
41          buffer.putInt(0, streamId & 0x7F_FF_FF_FF);
42          buffer.putInt(4, read & 0x00_FF_FF_FF);
43  
44          byte flags = dataInfo.getFlags();
45          if (dataInfo.available() > 0)
46              flags &= ~DataInfo.FLAG_CLOSE;
47          buffer.put(4, flags);
48  
49          buffer.flip();
50          return buffer;
51      }
52  }