View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http2.parser;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.http2.frames.PriorityFrame;
24  
25  public class HeaderBlockFragments
26  {
27      private PriorityFrame priorityFrame;
28      private boolean endStream;
29      private int streamId;
30      private ByteBuffer storage;
31  
32      public void storeFragment(ByteBuffer fragment, int length, boolean last)
33      {
34          if (storage == null)
35          {
36              int space = last ? length : length * 2;
37              storage = ByteBuffer.allocate(space);
38          }
39  
40          // Grow the storage if necessary.
41          if (storage.remaining() < length)
42          {
43              int space = last ? length : length * 2;
44              int capacity = storage.position() + space;
45              ByteBuffer newStorage = ByteBuffer.allocate(capacity);
46              storage.flip();
47              newStorage.put(storage);
48              storage = newStorage;
49          }
50  
51          // Copy the fragment into the storage.
52          int limit = fragment.limit();
53          fragment.limit(fragment.position() + length);
54          storage.put(fragment);
55          fragment.limit(limit);
56      }
57  
58      public PriorityFrame getPriorityFrame()
59      {
60          return priorityFrame;
61      }
62  
63      public void setPriorityFrame(PriorityFrame priorityFrame)
64      {
65          this.priorityFrame = priorityFrame;
66      }
67  
68      public boolean isEndStream()
69      {
70          return endStream;
71      }
72  
73      public void setEndStream(boolean endStream)
74      {
75          this.endStream = endStream;
76      }
77  
78      public ByteBuffer complete()
79      {
80          ByteBuffer result = storage;
81          storage = null;
82          result.flip();
83          return result;
84      }
85  
86      public int getStreamId()
87      {
88          return streamId;
89      }
90  
91      public void setStreamId(int streamId)
92      {
93          this.streamId = streamId;
94      }
95  }