如何在Java中找到ReadableByteChannel的大小? [英] How to find the size of a ReadableByteChannel in Java?
问题描述
我正在使用Java的Channel类将blob从数据库转换为PDF.使用频道的
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.
当ReadableByteChannels没有像其他Channels一样具有size()方法时,应该如何找到最大字节数?
How are you supposed to find this maximum number of bytes when ReadableByteChannels don't have a size( ) method like other Channels?
由于ReadableByteChannel没有size()方法,因此它不是该作业的正确工具吗?我只是在使用Long.MAX_VALUE,看起来不太正确.
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.
示例代码:
// 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();
推荐答案
使用Long.MAX_VALUE,如果可以的话,请使用Blob
的长度.
Use Long.MAX_VALUE, or the length of the Blob
if you can get it.
"ReadableByteChannel
的长度"通常毫无意义.考虑一个SocketChannel
,其对等方永远不会关闭连接.
'Length of a ReadableByteChannel
' is meaningless in general. Consider a SocketChannel
whose peer never closes the connection.
这篇关于如何在Java中找到ReadableByteChannel的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!