Skip to content

Commit 843dfe3

Browse files
committed
Refactor
1 parent 2c6bf76 commit 843dfe3

File tree

5 files changed

+45
-47
lines changed

5 files changed

+45
-47
lines changed

src/android/org/eclipse/jetty/websocket/PerMessageDeflateExtension.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.zip.Deflater;
2828
import java.util.zip.Inflater;
2929

30-
import org.eclipse.jetty.io.Buffer;
3130
import org.eclipse.jetty.util.log.Log;
3231
import org.eclipse.jetty.util.log.Logger;
3332
import org.eclipse.jetty.websocket.WebSocket.FrameConnection;
@@ -46,7 +45,8 @@ public class PerMessageDeflateExtension implements Extension {
4645

4746
private static final Logger __log = Log.getLogger(PerMessageDeflateExtension.class.getName());
4847
private static final byte[] TAIL_BYTES = new byte[] { 0x00, 0x00, (byte) 0xff, (byte) 0xff };
49-
private static final int INITIAL_CAPACITY = 8192;
48+
private static final int TAIL_LENGTH = TAIL_BYTES.length;
49+
private static final int INITIAL_CAPACITY = 16384;
5050
private static final String EXTENSION = "permessage-deflate";
5151
private static final String CLIENT_NO_CONTEXT_TAKEOVER = "client_no_context_takeover";
5252
private static final String SERVER_NO_CONTEXT_TAKEOVER = "server_no_context_takeover";
@@ -55,52 +55,59 @@ public class PerMessageDeflateExtension implements Extension {
5555
private static class Zlib {
5656
protected Deflater _deflater;
5757
protected Inflater _inflater;
58-
protected WebSocketBuffer _buffer;
59-
protected byte[] _buf;
58+
protected byte[] _buffer;
6059
protected int _capcacity;
6160
protected boolean _deflaterReset;
6261
protected boolean _inflaterReset;
6362

6463
public Zlib(boolean deflaterReset, boolean inflaterReset) {
6564
_deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
6665
_inflater = new Inflater(true);
67-
_buffer = new WebSocketBuffer(INITIAL_CAPACITY);
68-
_buf = new byte[INITIAL_CAPACITY];
69-
_capcacity = INITIAL_CAPACITY;
66+
_buffer = new byte[INITIAL_CAPACITY];
67+
_capcacity = _buffer.length;
7068
_deflaterReset = deflaterReset;
7169
_inflaterReset = inflaterReset;
7270
}
7371

72+
public static byte[] appendTailBytes(byte[] array, int offset, int length) {
73+
byte[] buffer = Arrays.copyOfRange(array, offset, offset + length + TAIL_LENGTH);
74+
System.arraycopy(TAIL_BYTES, 0, buffer, length, TAIL_LENGTH);
75+
return buffer;
76+
}
77+
7478
public byte[] compress(byte[] b, int offset, int length) {
7579
int len;
76-
int index = 0;
80+
int position = 0;
7781

7882
_deflater.reset();
7983
_deflater.setInput(b, offset, length);
8084
_deflater.finish();
81-
while ((len = _deflater.deflate(_buf, index, _capcacity - index)) > 0) {
82-
if ((index += len) == _capcacity) {
83-
_buf = Arrays.copyOf(_buf, _capcacity <<= 1);
85+
while ((len = _deflater.deflate(_buffer, position, _capcacity - position)) > 0) {
86+
if ((position += len) == _capcacity) {
87+
_buffer = Arrays.copyOf(_buffer, _capcacity <<= 1);
8488
}
8589
}
86-
return Arrays.copyOf(_buf, index);
90+
return Arrays.copyOf(_buffer, position);
91+
}
92+
93+
public byte[] decompress(byte[] b) throws DataFormatException {
94+
return decompress(b, 0, b.length);
8795
}
8896

89-
public Buffer decompress(byte[] b, int offset, int length) throws DataFormatException {
97+
public byte[] decompress(byte[] b, int offset, int length) throws DataFormatException {
9098
int len;
91-
int index = 0;
99+
int position = 0;
92100

93101
if (_inflaterReset) {
94102
_inflater.reset();
95103
}
96104
_inflater.setInput(b, offset, length);
97-
while ((len = _inflater.inflate(_buf, index, _capcacity - index)) > 0) {
98-
if ((index += len) == _capcacity) {
99-
_buf = Arrays.copyOf(_buf, _capcacity <<= 1);
105+
while ((len = _inflater.inflate(_buffer, position, _capcacity - position)) > 0) {
106+
if ((position += len) == _capcacity) {
107+
_buffer = Arrays.copyOf(_buffer, _capcacity <<= 1);
100108
}
101109
}
102-
_buffer.clear();
103-
return _buffer.append(_buf, 0, index);
110+
return Arrays.copyOf(_buffer, position);
104111
}
105112

106113
public boolean isCompressible() {
@@ -122,18 +129,18 @@ public NewZlib(boolean deflaterReset, boolean inflaterReset) {
122129
@Override
123130
public byte[] compress(byte[] b, int offset, int length) {
124131
int len;
125-
int index = 0;
132+
int position = 0;
126133

127134
if (_deflaterReset) {
128135
_deflater.reset();
129136
}
130137
_deflater.setInput(b, offset, length);
131-
while ((len = _deflater.deflate(_buf, index, _capcacity - index, Deflater.SYNC_FLUSH)) > 0) {
132-
if ((index += len) == _capcacity) {
133-
_buf = Arrays.copyOf(_buf, _capcacity <<= 1);
138+
while ((len = _deflater.deflate(_buffer, position, _capcacity - position, Deflater.SYNC_FLUSH)) > 0) {
139+
if ((position += len) == _capcacity) {
140+
_buffer = Arrays.copyOf(_buffer, _capcacity <<= 1);
134141
}
135142
}
136-
return Arrays.copyOf(_buf, index - TAIL_BYTES.length);
143+
return Arrays.copyOf(_buffer, position - TAIL_LENGTH);
137144
}
138145

139146
@Override
@@ -159,26 +166,29 @@ public PerMessageDeflateExtension() {
159166
}
160167

161168
@Override
162-
public void onFrame(byte flags, byte opcode, Buffer buffer) {
169+
public void onFrame(byte flags, byte opcode, byte[] array, int offset, int length) {
163170
switch (opcode) {
164171
case WebSocketConnectionRFC6455.OP_TEXT:
165172
case WebSocketConnectionRFC6455.OP_BINARY:
166173
_compressed = ((flags & 0x07) == 0x04);
167174
case WebSocketConnectionRFC6455.OP_CONTINUATION:
168175
if (_compressed) {
169176
try {
177+
byte[] buffer;
170178
if ((flags &= 0x08) > 0) {
171-
buffer.put(TAIL_BYTES);
179+
buffer = _zlib.decompress(Zlib.appendTailBytes(array, offset, length));
180+
} else {
181+
buffer = _zlib.decompress(array, offset, length);
172182
}
173-
_inbound.onFrame(flags, opcode, _zlib.decompress(buffer.array(), buffer.getIndex(), buffer.length()));
183+
_inbound.onFrame(flags, opcode, buffer, 0, buffer.length);
174184
} catch (DataFormatException e) {
175185
__log.warn(e);
176186
_connection.close(WebSocketConnectionRFC6455.CLOSE_BAD_DATA, "Bad data");
177187
}
178188
return;
179189
}
180190
}
181-
_inbound.onFrame(flags, opcode, buffer);
191+
_inbound.onFrame(flags, opcode, array, offset, length);
182192
}
183193

184194
@Override

src/android/org/eclipse/jetty/websocket/WebSocketBuffer.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,9 @@ class WebSocketBuffer implements Buffer {
3636

3737
public WebSocketBuffer(final int capacity) {
3838
_buffer = new byte[capacity];
39-
_index = 0;
4039
_capacity = capacity;
4140
}
4241

43-
public WebSocketBuffer(final byte[] buffer, final int offset, final int length) {
44-
_buffer = Arrays.copyOfRange(buffer, offset, length);
45-
_index = length;
46-
_capacity = length;
47-
}
48-
4942
public WebSocketBuffer append(final byte[] array, final int offset, final int length) {
5043
if ((_index += length) > _capacity) {
5144
_buffer = Arrays.copyOf(_buffer, (_capacity = Math.max(_capacity << 1, _index)));

src/android/org/eclipse/jetty/websocket/WebSocketConnectionRFC6455.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ private boolean excess(int opcode, int length)
655655
}
656656
}
657657

658-
public void onFrame(final byte flags, final byte opcode, final Buffer buffer)
658+
public void onFrame(final byte flags, final byte opcode, final byte[] array, int offset, int length)
659659
{
660660
synchronized (WebSocketConnectionRFC6455.this)
661661
{
@@ -666,10 +666,6 @@ public void onFrame(final byte flags, final byte opcode, final Buffer buffer)
666666
}
667667
}
668668

669-
byte[] array = buffer.array();
670-
int offset = buffer.getIndex();
671-
int length = buffer.length();
672-
673669
if (isControlFrame(opcode) && length > MAX_CONTROL_FRAME_PAYLOAD)
674670
{
675671
errorClose(WebSocketConnectionRFC6455.CLOSE_PROTOCOL, "Control frame too large: " + length + " > " + MAX_CONTROL_FRAME_PAYLOAD);
@@ -816,7 +812,7 @@ public void onFrame(final byte flags, final byte opcode, final Buffer buffer)
816812
return;
817813
}
818814
}
819-
else if(buffer.length() == 1)
815+
else if(length == 1)
820816
{
821817
// Invalid length. use status code 1002 (Protocol error)
822818
errorClose(WebSocketConnectionRFC6455.CLOSE_PROTOCOL,"Invalid payload length of 1");

src/android/org/eclipse/jetty/websocket/WebSocketParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/**
2525
* Parser the WebSocket protocol.
2626
*
27+
* modified by KNOWLEDGECODE
2728
*/
2829
public interface WebSocketParser
2930
{
@@ -32,7 +33,7 @@ public interface WebSocketParser
3233
/* ------------------------------------------------------------ */
3334
public interface FrameHandler
3435
{
35-
void onFrame(byte flags, byte opcode, Buffer buffer);
36+
void onFrame(byte flags, byte opcode, byte[] array, int offset, int length);
3637
void close(int code,String message);
3738
}
3839

src/android/org/eclipse/jetty/websocket/WebSocketParserRFC6455.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
/**
3131
* Parser the WebSocket protocol.
3232
*
33+
* modified by KNOWLEDGECODE
3334
*/
3435
public class WebSocketParserRFC6455 implements WebSocketParser
3536
{
@@ -161,10 +162,9 @@ public int parseNext()
161162
array[i]^=_mask[_m++%4];
162163
}
163164

164-
// System.err.printf("%s %s %s >>\n",TypeUtil.toHexString(_flags),TypeUtil.toHexString(_opcode),data.length());
165165
_bytesNeeded-=data.length();
166166
progress=true;
167-
_handler.onFrame((byte)(_flags&(0xff^WebSocketConnectionRFC6455.FLAG_FIN)), _opcode, data);
167+
_handler.onFrame((byte)(_flags&(0xff^WebSocketConnectionRFC6455.FLAG_FIN)), _opcode, data.array(), data.getIndex(), data.length());
168168

169169
_opcode=WebSocketConnectionRFC6455.OP_CONTINUATION;
170170
}
@@ -341,10 +341,8 @@ public int parseNext()
341341
array[i]^=_mask[_m++%4];
342342
}
343343

344-
// System.err.printf("%s %s %s >>\n",TypeUtil.toHexString(_flags),TypeUtil.toHexString(_opcode),data.length());
345-
346344
progress=true;
347-
_handler.onFrame(_flags, _opcode, data);
345+
_handler.onFrame(_flags, _opcode, data.array(), data.getIndex(), data.length());
348346
_bytesNeeded=0;
349347
_state=State.START;
350348
}

0 commit comments

Comments
 (0)