View Javadoc

1   //========================================================================
2   //Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //All rights reserved. This program and the accompanying materials
5   //are made available under the terms of the Eclipse Public License v1.0
6   //and Apache License v2.0 which accompanies this distribution.
7   //The Eclipse Public License is available at
8   //http://www.eclipse.org/legal/epl-v10.html
9   //The Apache License v2.0 is available at
10  //http://www.opensource.org/licenses/apache2.0.php
11  //You may elect to redistribute this code under either of these licenses.
12  //========================================================================
13  
14  package org.eclipse.jetty.spdy.parser;
15  
16  import java.nio.ByteBuffer;
17  
18  public class UnknownControlFrameBodyParser extends ControlFrameBodyParser
19  {
20      private final ControlFrameParser controlFrameParser;
21      private State state = State.BODY;
22      private int remaining;
23  
24      public UnknownControlFrameBodyParser(ControlFrameParser controlFrameParser)
25      {
26          this.controlFrameParser = controlFrameParser;
27      }
28  
29      @Override
30      public boolean parse(ByteBuffer buffer)
31      {
32          switch (state)
33          {
34              case BODY:
35              {
36                  remaining = controlFrameParser.getLength();
37                  state = State.CONSUME;
38                  // Fall down
39              }
40              case CONSUME:
41              {
42                  int consume = Math.min(remaining, buffer.remaining());
43                  buffer.position(buffer.position() + consume);
44                  remaining -= consume;
45                  if (remaining > 0)
46                      return false;
47                  reset();
48                  return true;
49              }
50              default:
51              {
52                  throw new IllegalStateException();
53              }
54          }
55      }
56  
57      private void reset()
58      {
59          state = State.BODY;
60          remaining = 0;
61      }
62  
63      private enum State
64      {
65          BODY, CONSUME
66      }
67  }