5

Is there a way I can get netty ByteBuf from a java File object? I am trying to use Custom Snappy class to validate checksum. Below is my code where I'd like to get File object in bytebuf variable.

import io.netty.buffer.ByteBuf;

File file = new File("boot.zip");
ByteBuf byteBuf = Unpooled.buffer(128);
byteBuf.writeBytes(new FileInputStream(file));
int expectedChecksum = Snappy.calculateChecksum(byteBuf);
logger.info("checksum = "+expectedChecksum);

boolean checksumValid = true;
try {       
    CustomSnappy.validateChecksum(expectedChecksum, byteBuf);
} catch(DecompressionException e) {
    checksumValid = false;
    e.printStackTrace();
    logger.error("DecompressionException in checksum calculation " + e);            
} catch(Exception e) {
    checksumValid = false;
    e.printStackTrace();
    logger.error("Exception in checksum calculation " + e);
}

I used Unpooled.buffer(128) but it gives me No signature of method :io.netty.buffer.UnpooledHeapByteBuf.writeBytes() is applicable for argument types: (java.io.FileInputStream) values: [java.io.FileInputStream@28b6520b]

4 Answers 4

6

You can created a MappedByteBuf from a FileChannel and wrap it in ByteBuffer via Unpooled.wrappedBuffer(..).

Sign up to request clarification or add additional context in comments.

Comments

5

Here is an example of how to "create a MappedByteBuf from a FileChannel and wrap it in ByteBuffer via Unpooled.wrappedBuffer(..)":

    try {
        File file = new File(filename);
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel fileChannel = fileInputStream.getChannel();
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());

        ByteBuf byteBuf = Unpooled.wrappedBuffer(mappedByteBuffer);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Comments

2

You can use the method ByteBuf#writeBytes(InputStream). This should work, but I did not test it.

ByteBuf buffer = // see http://stackoverflow.com/a/15088238/834
buffer.writeBytes(new FileInputStream(file), file.length());

See http://netty.io/4.0/api/io/netty/buffer/ByteBuf.html#writeBytes(java.io.InputStream,%20int)

The documentation shows how to create a new ByteBuf: http://netty.io/4.0/api/io/netty/buffer/Unpooled.html

8 Comments

I get cannot create ByteBuf object from abstract type
Updated the answer with links to the official documentation and a good answer on SO.
but I cannot find anything to use with File object in your link.
Of course not, you have to create a new ByteBuf, then write the bytes from your file into the buffer, most likely with code like I tried to provide.
I used Unpooled.buffer(128) but it gives me No signature of method: io.netty.buffer.UnpooledHeapByteBuf.writeBytes() is applicable for argument types: (java.io.FileInputStream) values: [java.io.FileInputStream@28b6520b] exception
|
0

If you are using JDK 1.7, the class of java.nio.file.Files could make it easy with amount of static methods.

ByteBuf buffer = Unpooled.copiedBuffer(Files.readAllBytes(new File(this.getClass().getResource("/requests/overPackageRequest").getFile()).toPath()));

1 Comment

Files.readAllBytes cannot deal with files that are over Integer.MAX_VALUE bytes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.