1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.common.test;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import org.junit.Assert;
public class RawFrameBuilder
{
public static void putOpFin(ByteBuffer buf, byte opcode, boolean fin)
{
byte b = 0x00;
if (fin)
{
b |= 0x80;
}
b |= opcode & 0x0F;
buf.put(b);
}
public static void putLengthAndMask(ByteBuffer buf, int length, byte mask[])
{
if (mask != null)
{
Assert.assertThat("Mask.length",mask.length,is(4));
putLength(buf,length,(mask != null));
buf.put(mask);
}
else
{
putLength(buf,length,false);
}
}
public static byte[] mask(final byte[] data, final byte mask[])
{
Assert.assertThat("Mask.length",mask.length,is(4));
int len = data.length;
byte ret[] = new byte[len];
System.arraycopy(data,0,ret,0,len);
for (int i = 0; i < len; i++)
{
ret[i] ^= mask[i % 4];
}
return ret;
}
public static void putLength(ByteBuffer buf, int length, boolean masked)
{
if (length < 0)
{
throw new IllegalArgumentException("Length cannot be negative");
}
byte b = (masked?(byte)0x80:0x00);
// write the uncompressed length
if (length > 0xFF_FF)
{
buf.put((byte)(b | 0x7F));
buf.put((byte)0x00);
buf.put((byte)0x00);
buf.put((byte)0x00);
buf.put((byte)0x00);
buf.put((byte)((length >> 24) & 0xFF));
buf.put((byte)((length >> 16) & 0xFF));
buf.put((byte)((length >> 8) & 0xFF));
buf.put((byte)(length & 0xFF));
}
else if (length >= 0x7E)
{
buf.put((byte)(b | 0x7E));
buf.put((byte)(length >> 8));
buf.put((byte)(length & 0xFF));
}
else
{
buf.put((byte)(b | length));
}
}
public static void putMask(ByteBuffer buf, byte mask[])
{
Assert.assertThat("Mask.length",mask.length,is(4));
buf.put(mask);
}
public static void putPayloadLength(ByteBuffer buf, int length)
{
putLength(buf,length,true);
}
}
|