2 years ago

#44094

test-img

heph2

How to properly read data from a SSLSocket and write them into a ByteBuffer

I'm trying to get data from TCP SSL Socket and write them into a ByteBuffer ( For "seekability" purposes ). Currently i'm doing this way:

private SSLSocket sock;
private ByteBuffer bf;
    this.bf = ByteBuffer.allocate(bufSize);
    BufferedInputStream inFromClient = new BufferedInputStream
        (this.sock.getInputStream());
        
    while (true) {
        int n = inFromClient.read();

        if (n == -1) {
            break;
        }

        bf.put((byte) n);
    }

But apparently the data is not written correctly and store only NUL byte. If i try to read each byte directly from BufferedInputStream, i can see the data without issues:

System.out.println((char) n);

Instead the ByteBuffer is full of NUL bytes..

What i'm doing wrong in this case? And also is this the correct modus operandi for reading from Socket to ByteBuffer?

Thanks in advance :D

java

sockets

bytebuffer

0 Answers

Your Answer

Accepted video resources