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]

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192

4 Answers4

6

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

Norman Maurer
  • 23,104
  • 2
  • 33
  • 31
4

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();
    }
jox
  • 2,218
  • 22
  • 32
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

cringe
  • 13,401
  • 15
  • 69
  • 102
  • I get cannot create `ByteBuf` object from abstract type –  Dec 20 '14 at 18:31
  • Updated the answer with links to the official documentation and a good answer on SO. – cringe Dec 20 '14 at 18:33
  • but I cannot find anything to use with File object in your link. –  Dec 20 '14 at 18:40
  • 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. – cringe Dec 20 '14 at 18:41
  • 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 –  Dec 20 '14 at 18:47
  • You should add more code and the new exceptions to your question. – cringe Dec 20 '14 at 18:49
  • As documented, there is no `writeBytes` method accepting one parameter, `java.io.InputStream`, only. This answer is confusing without deeply reading and understanding the official document. – 千木郷 Oct 24 '18 at 18:12
  • `writeBytes` takes length as int `int writeBytes(InputStream in, int length)` – prayagupa Jun 04 '21 at 17:32
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,595
  • 2
  • 19
  • 30