1 package org.eclipse.jetty.websocket; 2 3 import java.io.IOException; 4 import java.util.Map; 5 6 public class FragmentExtension extends AbstractExtension 7 { 8 private int _maxLength=-1; 9 private int _minFragments=1; 10 11 public FragmentExtension() 12 { 13 super("fragment"); 14 } 15 16 @Override 17 public boolean init(Map<String, String> parameters) 18 { 19 if(super.init(parameters)) 20 { 21 _maxLength=getInitParameter("maxLength",_maxLength); 22 _minFragments=getInitParameter("minFragments",_minFragments); 23 return true; 24 } 25 return false; 26 } 27 28 @Override 29 public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException 30 { 31 if (getConnection().isControl(opcode)) 32 { 33 super.addFrame(flags,opcode,content,offset,length); 34 return; 35 } 36 37 int fragments=1; 38 39 while (_maxLength>0 && length>_maxLength) 40 { 41 fragments++; 42 super.addFrame((byte)(flags&~getConnection().finMask()),opcode,content,offset,_maxLength); 43 length-=_maxLength; 44 offset+=_maxLength; 45 opcode=getConnection().continuationOpcode(); 46 } 47 48 while (fragments<_minFragments) 49 { 50 int frag=length/2; 51 fragments++; 52 super.addFrame((byte)(flags&0x7),opcode,content,offset,frag); 53 length-=frag; 54 offset+=frag; 55 opcode=getConnection().continuationOpcode(); 56 } 57 58 super.addFrame((byte)(flags|getConnection().finMask()),opcode,content,offset,length); 59 } 60 61 62 }