add text and binary frame generators

This commit is contained in:
Jesse McConnell 2012-06-20 16:34:45 -05:00
parent b61a58ad64
commit 8893b9c96a
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.BinaryFrame;
public class BinaryFrameGenerator extends FrameGenerator<BinaryFrame>
{
public BinaryFrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy policy)
{
super(bufferPool, policy);
}
@Override
public ByteBuffer payload(BinaryFrame binary)
{
return binary.getData();
}
}

View File

@ -0,0 +1,35 @@
package org.eclipse.jetty.websocket.generator;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.TextFrame;
public class TextFrameGenerator extends FrameGenerator<TextFrame>
{
public TextFrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy policy)
{
super(bufferPool, policy);
}
@Override
public ByteBuffer payload(TextFrame text)
{
try
{
String data = text.getData().toString();
ByteBuffer payload = ByteBuffer.allocate(data.length());
payload.put(data.getBytes("UTF-8"));
return payload;
}
catch (UnsupportedEncodingException e)
{
// TODO improve ex handling
throw new WebSocketException("text frame was not correctly encoded");
}
}
}