If you created your pipe like this:
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
Is there any way to get the raw data from a pipeline that usually handles text?
I’d really like to do this:
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
// Save data received from the server.
Object msg = e.getMessage();
byte[] rawdata = new byte[((ChannelBuffer)msg).readableBytes()];
((ChannelBuffer)msg).getBytes(0, rawdata);
tmpTarFile.write(rawdata);
}
The channel is mostly text, but sometimes I need to read raw binary out of it.
In this case the msg that comes in is a String object because of the StringDecoder in the pipeline. I’d like to get the channelbuffer data beneath that string…
On the server side, the data was written with this:
ChannelBuffer databuffer = ChannelBuffers.buffer(blobstream.size());
databuffer.writeBytes(blobstream.toByteArray());
e.getChannel().write(databuffer);
Looks like I have to turn off the stringencoder/decoder (which converts it into a ChannelBuffer w/ raw byte access) and convert bytes to/from Strings…
If you want to get the raw ChannelBuffer you will need to remove the Decoder from the pipeline and then once you want to handle String’s again just add it back.
You could also extend the StringDecoder and based on some logic decode it or not.
Something like that: