将 StringDecoder 设置为管道解码器后获取二进制数据

发布于 2024-12-25 16:10:56 字数 948 浏览 1 评论 0原文

如果您像这样创建管道:

    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

有什么方法可以从通常处理文本的管道中获取原始数据? 我真的很想这样做:

    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);
    }

该频道主要是文本,但有时我需要从中读取原始二进制文件。 在这种情况下,由于管道中存在 StringDecoder,传入的 msg 是一个 String 对象。我想获取该字符串下方的通道缓冲区数据...

在服务器端,数据是这样写入的:

        ChannelBuffer databuffer = ChannelBuffers.buffer(blobstream.size());
        databuffer.writeBytes(blobstream.toByteArray());
        e.getChannel().write(databuffer);

看起来我必须关闭字符串编码器/解码器(它将其转换为带有原始字节访问的 ChannelBuffer) )并将字节与字符串相互转换...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-01-01 16:10:56

如果您想获取原始 ChannelBuffer,您需要从管道中删除解码器,然后一旦您想再次处理 String,只需将其添加回来。

您还可以扩展 StringDecoder 并基于某些逻辑对其进行解码或不解码。
像这样的东西:

public class FlexibleStringDecoder extends StringDecoder {

    @Override
    protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
        if (decodeToString(msg)) {
            return super.decode(ctx, channel, msg);
        }
        return msg;
    }

    public boolean decodeToString(Object msg) {
        // Add some logic here....
        return true;
    }

}

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:

public class FlexibleStringDecoder extends StringDecoder {

    @Override
    protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
        if (decodeToString(msg)) {
            return super.decode(ctx, channel, msg);
        }
        return msg;
    }

    public boolean decodeToString(Object msg) {
        // Add some logic here....
        return true;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文