Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:08:44+00:00 2026-06-05T01:08:44+00:00

I want to write raw bytes to a netty Channel. I thought I could

  • 0

I want to write raw bytes to a netty Channel. I thought I could do this by first creating a ChannelBuffer, filling it with bytes (coming from, say, a Kryo serializer), and then writing that ChannelBuffer to the netty Channel.

Here is the relevant code, where I’m simply filling some bytes into a ChannelBuffer in the ctor and trying to send it upon connection:

/**
 * Handler implementation for the object echo client.  It initiates the
 * ping-pong traffic between the object echo client and server by sending the
 * first message to the server.
 */
public class ObjectEchoClientHandler extends SimpleChannelUpstreamHandler {

private final ChannelBuffer firstMessage;

/**
 * Creates a client-side handler.
 */
public ObjectEchoClientHandler(int firstMessageSize) {
    if (firstMessageSize <= 0) {
        throw new IllegalArgumentException(
                "firstMessageSize: " + firstMessageSize);
    }
    firstMessage = ChannelBuffers.buffer(8192);
    for (int i = 0; i < firstMessageSize; i++) {
        firstMessage.writeByte(i % 256);
    }
}

@Override
public void channelConnected(
        ChannelHandlerContext ctx, ChannelStateEvent e) {

    // Send the first message if this handler is a client-side handler.
    // e.getChannel().write(firstMessage);
    Channels.write(ctx, e.getFuture(), firstMessage);
}

}

This doesn’t seem to work, as it throws this exception:

java.io.NotSerializableException: org.jboss.netty.buffer.BigEndianHeapChannelBuffer
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at org.jboss.netty.handler.codec.serialization.ObjectEncoder.encode(ObjectEncoder.java:80)
at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:61)
at org.jboss.netty.channel.Channels.write(Channels.java:626)
at org.jboss.netty.channel.Channels.write(Channels.java:587)
at ca.gsimard.spacecraft.client.ObjectEchoClientHandler.channelConnected(ObjectEchoClientHandler.java:83)
at ca.gsimard.spacecraft.client.ObjectEchoClientHandler.handleUpstream(ObjectEchoClientHandler.java:75)
at org.jboss.netty.channel.Channels.fireChannelConnected(Channels.java:227)
at org.jboss.netty.channel.socket.nio.NioWorker$RegisterTask.run(NioWorker.java:784)
at org.jboss.netty.channel.socket.nio.NioWorker.processRegisterTaskQueue(NioWorker.java:250)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:192)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

I’m sure this is dead trivial and probably the wrong way of doing it, because I can’t seem to find anyone who had the same error as this. I’m using Netty 3.3.0 right now.

I can get this to work by creating a properly sized byte[] and copying the contents of the ChannelBuffer to it, but I would have liked a more direct solution that avoids useless byte copying.

    byte[] array = new byte[firstMessage.writerIndex()];
    firstMessage.readBytes(array);
    e.getChannel().write(array);

Thanks for any pointers !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:08:46+00:00Added an answer on June 5, 2026 at 1:08 am

    Looking at your stack trace, I observed these two lines:

    at org.jboss.netty.handler.codec.serialization.ObjectEncoder.encode(ObjectEncoder.java:80)
    at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:61
    

    What this means to me is that your pipeline contains an ObjectEncoder which is attempting to serialize a ChannelBuffer. Since your objects have already been encoded by Kryo, you don’t need the ObjectEncoder.

    Or put differently (without Kryo), a ChannelBuffer should be the output of the ObjectEncoder and your object should be the input.

    Your best bet is to write a new ChannelHandler that uses Kryo to encode your objects. You may find some added efficiencies when Kryo does it’s job as a handler in the Netty pipeline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a raw email, (MIME multipart), and I want to display this on
I am new to raw socket. I want write a server and a client
I want to write a byte array to a raw column in oracle. byte[]
I want to write a raw byte/byte stream to a position in a file.
I want to read from the text file which is placed in res/raw/text i
I want to write a byte array to file. This is code in the
i want to write a program using multi-threading, raw sockets, to scan the ports
Here's my code: sentenceToTranslate = raw_input(Please write in the sentence you want to translate:
I want write a little code analyzer which parses nested structures and translates into
Hello I want write my own desktop sharing application in Java. The application should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.