1

I am converting a blob from a database into a PDF, using Java's Channel classes. When using a Channel's transferFrom( ) method, you are supposed to specify the maximum number of bytes to be transferred.

How are you supposed to find this maximum number of bytes when ReadableByteChannels don't have a size( ) method like other Channels?

Is a ReadableByteChannel not the right tool for the job since it doesn't have a size( ) method? I'm just using Long.MAX_VALUE which doesn't seem right.

Example code:

// Get the blob in an InputStream
InputStream inStream = rs.getBinaryStream("SomeBlob");

// Create a ReadableByteChannel from that InputStream
ReadableByteChannel rbc = Channels.newChannel(inStream);

// Make a FileOutputStream to write the PDF From
FileOutputStream outStream = new FileOutputStream("MyPDF.PDF");
outStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

// Close stuff
inStream.close();
outStream.close();
rbc.close();
jabe
  • 784
  • 2
  • 15
  • 33

1 Answers1

2

Use Long.MAX_VALUE, or the length of the Blob if you can get it.

'Length of a ReadableByteChannel' is meaningless in general. Consider a SocketChannel whose peer never closes the connection.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Long.MAX_VALUE is huge, but my program seems to run fine with it. Does it attempt to write Long.MAX_VALUE bytes or does it just go until there aren't any left? That is my concern. – jabe Jan 29 '16 at 19:22
  • That question is already answered in the Javadoc you cited. It stops at the end of the input. What else could it do? It can't transfer bytes that don't exist. – user207421 Jan 29 '16 at 19:23