View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2011 Intalio, Inc.
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    *
8    *   The Eclipse Public License is available at
9    *   http://www.eclipse.org/legal/epl-v10.html
10   *
11   *   The Apache License v2.0 is available at
12   *   http://www.opensource.org/licenses/apache2.0.php
13   *
14   * You may elect to redistribute this code under either of these licenses.
15   *******************************************************************************/
16  package org.eclipse.jetty.websocket;
17  
18  import java.io.IOException;
19  import java.util.Map;
20  
21  public class FragmentExtension extends AbstractExtension
22  {
23      private int _maxLength=-1;
24      private int _minFragments=1;
25      
26      public FragmentExtension()
27      {
28          super("fragment");
29      }
30  
31      @Override
32      public boolean init(Map<String, String> parameters)
33      {
34          if(super.init(parameters))
35          {
36              _maxLength=getInitParameter("maxLength",_maxLength);
37              _minFragments=getInitParameter("minFragments",_minFragments);
38              return true;
39          }
40          return false;
41      }
42  
43      @Override
44      public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException
45      {
46          if (getConnection().isControl(opcode))
47          {
48              super.addFrame(flags,opcode,content,offset,length);
49              return;
50          }
51          
52          int fragments=1;
53          
54          while (_maxLength>0 && length>_maxLength)
55          {
56              fragments++;
57              super.addFrame((byte)(flags&~getConnection().finMask()),opcode,content,offset,_maxLength);
58              length-=_maxLength;
59              offset+=_maxLength;
60              opcode=getConnection().continuationOpcode();
61          }
62          
63          while (fragments<_minFragments)
64          {
65              int frag=length/2;
66              fragments++;
67              super.addFrame((byte)(flags&0x7),opcode,content,offset,frag);
68              length-=frag;
69              offset+=frag;
70              opcode=getConnection().continuationOpcode();
71          }
72  
73          super.addFrame((byte)(flags|getConnection().finMask()),opcode,content,offset,length);
74      }
75      
76      
77  }