Java - problems with ByteBuffer.remaining ()

Summary: I have a ByteBuffer in which I upload some data. After that I want to send this data via Socket.

So, I wrote the code as follows:

    private static void serialize(ByteBuffer buffer, EmployeeData emp, CharsetEncoder encoder)
{
    // id
    buffer.putInt(emp.getId());

    CharBuffer nameBuffer = CharBuffer.wrap(emp.getFirstName().toCharArray());
    ByteBuffer nbBuffer = null;

    // length of first name
    try
    {
        nbBuffer = encoder.encode(nameBuffer);
    } 
    catch(CharacterCodingException e)
    {
        throw new ArithmeticException();
    }

    System.out.println(String.format("String [%1$s] #bytes = %2$s", emp.getFirstName(), nbBuffer.limit()));
    buffer.putInt(nbBuffer.limit());
    buffer.put(nbBuffer);

    // put lastname
    nameBuffer = CharBuffer.wrap(emp.getLastName().toCharArray());
    nbBuffer = null;

    // length of first name
    try
    {
        nbBuffer = encoder.encode(nameBuffer);          
    } 
    catch(CharacterCodingException e)
    {
        throw new ArithmeticException();
    }

    System.out.println(String.format("String [%1$s] #bytes = %2$s", emp.getLastName(), nbBuffer.limit()));
    buffer.putInt(nbBuffer.limit());
    buffer.put(nbBuffer);

    // salary
    buffer.putInt(emp.getSalary());
}

In the calling code, I do the following. First I get a serialized ByteBuffer and then write it to the socket ...

            Socket client = new Socket("localhost", 8080);

        OutputStream oStream = client.getOutputStream();

        serialize(buffer, emp, encoder);

        buffer.rewind();            
        while(buffer.hasRemaining())
        {
            byte temp = buffer.get();
            ++ written;
        }

        buffer.rewind();
        System.out.println("#Bytes in output buffer: " + written + " limit = " + buffer.limit() + " pos = " + buffer.position() + " remaining = " + buffer.remaining());

        int remaining = buffer.remaining();
        while(remaining > 0)
        {
            oStream.write(buffer.get());
            -- remaining;
        }

I expect buffer.remaining () to be exactly equal to #bytes I being uploaded to the buffer. However, I find that it is not, it is always equal to 1024, which is the size of the underlying buffer array.

This is how I create the buffer ...

        Charset charset = Charset.forName("UTF-8");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    byte [] underlyingBuffer = new byte[1024];
    ByteBuffer buffer = ByteBuffer.wrap(underlyingBuffer);
    buffer.order(ByteOrder.LITTLE_ENDIAN);

This is what I get as the output of my print statement ...

String [John] #bytes = 4 String [Smith] #bytes = 5

#Bytes in output buffer: 1024 limit = 1024 pos = 0 left = 1024

How can I get the exact # bytes that were buffered?

Thanks!

+3
1

, flip . , rewind.

A java.nio.Buffer capacity limit. , , .

, , , - 1024. ByteBuffer#remaining .

+11

Source: https://habr.com/ru/post/1722898/


All Articles