use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class DeflateFrameExtensionTest method assertOutgoing.
private void assertOutgoing(String text, String expectedHex) throws IOException {
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
DeflateFrameExtension ext = new DeflateFrameExtension();
ext.setBufferPool(bufferPool);
ext.setPolicy(policy);
ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
ext.setConfig(config);
Generator generator = new Generator(policy, bufferPool, true);
generator.configureFromExtensions(Collections.singletonList(ext));
OutgoingNetworkBytesCapture capture = new OutgoingNetworkBytesCapture(generator);
ext.setNextOutgoingFrames(capture);
Frame frame = new TextFrame().setPayload(text);
ext.outgoingFrame(frame, null, BatchMode.OFF);
capture.assertBytes(0, expectedHex);
}
use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class UnitGenerator method generate.
/**
* Generate a single giant buffer of all provided frames Not appropriate for production code, but useful for testing.
* @param frames the list of frames to generate from
* @return the bytebuffer representing all of the generated frames
*/
public static ByteBuffer generate(List<WebSocketFrame> frames) {
// Create non-symmetrical mask (helps show mask bytes order issues)
byte[] MASK = { 0x11, 0x22, 0x33, 0x44 };
// the generator
Generator generator = new UnitGenerator();
// Generate into single bytebuffer
int buflen = 0;
for (Frame f : frames) {
buflen += f.getPayloadLength() + Generator.MAX_HEADER_LENGTH;
}
ByteBuffer completeBuf = ByteBuffer.allocate(buflen);
BufferUtil.clearToFill(completeBuf);
// Generate frames
for (WebSocketFrame f : frames) {
// make sure we have the test mask set
f.setMask(MASK);
BufferUtil.put(generator.generateHeaderBytes(f), completeBuf);
ByteBuffer window = f.getPayload();
if (BufferUtil.hasContent(window)) {
BufferUtil.put(window, completeBuf);
}
}
BufferUtil.flipToFlush(completeBuf, 0);
if (LOG.isDebugEnabled()) {
LOG.debug("generate({} frames) - {}", frames.size(), BufferUtil.toDetailString(completeBuf));
}
return completeBuf;
}
use of org.eclipse.jetty.websocket.common.Generator in project jetty.project by eclipse.
the class UnitGenerator method generate.
/**
* Generate All Frames into a single ByteBuffer.
* <p>
* This is highly inefficient and is not used in production! (This exists to make testing of the Generator easier)
*
* @param frames
* the frames to generate from
* @return the ByteBuffer representing all of the generated frames provided.
*/
public static ByteBuffer generate(Frame[] frames) {
Generator generator = new UnitGenerator();
// Generate into single bytebuffer
int buflen = 0;
for (Frame f : frames) {
buflen += f.getPayloadLength() + Generator.MAX_HEADER_LENGTH;
}
ByteBuffer completeBuf = ByteBuffer.allocate(buflen);
BufferUtil.clearToFill(completeBuf);
// Generate frames
for (Frame f : frames) {
generator.generateWholeFrame(f, completeBuf);
}
BufferUtil.flipToFlush(completeBuf, 0);
if (LOG.isDebugEnabled()) {
LOG.debug("generate({} frames) - {}", frames.length, BufferUtil.toDetailString(completeBuf));
}
return completeBuf;
}
Aggregations