diff options
author | Sven Gothel <[email protected]> | 2013-02-01 02:16:08 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-02-01 02:16:08 +0100 |
commit | ec7f7a3c809bb9e3beb84ce90e2fcbd8b7f4b7ee (patch) | |
tree | 13d95f35175815952bdec0f53b8c17fee7f3fabf /src/java | |
parent | b47d0d92dd222999bf38633de1cec8de6a7ad369 (diff) |
IOUtil.copyStream2ByteBuffer: read while numRead > -1 ; add variant w/ initialCapcity
Diffstat (limited to 'src/java')
-rw-r--r-- | src/java/com/jogamp/common/util/IOUtil.java | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index a441c18..c3d3345 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -207,15 +207,30 @@ public class IOUtil { /** * Copy the specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned. * <p>The implementation creates the ByteBuffer w/ {@link #copyStream2ByteArray(InputStream)}'s returned byte array.</p> + * + * @param stream input stream, which will be wrapped into a BufferedInputStream, if not already done. */ public static ByteBuffer copyStream2ByteBuffer(InputStream stream) throws IOException { - // FIXME: Shall enforce a BufferedInputStream ? + return copyStream2ByteBuffer(stream, -1); + } + + /** + * Copy the specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned. + * <p>The implementation creates the ByteBuffer w/ {@link #copyStream2ByteArray(InputStream)}'s returned byte array.</p> + * + * @param stream input stream, which will be wrapped into a BufferedInputStream, if not already done. + * @param initialCapacity initial buffer capacity in bytes, if > available bytes + */ + public static ByteBuffer copyStream2ByteBuffer(InputStream stream, int initialCapacity) throws IOException { if( !(stream instanceof BufferedInputStream) ) { stream = new BufferedInputStream(stream); } int avail = stream.available(); + if( initialCapacity < avail ) { + initialCapacity = avail; + } final MachineDescription machine = Platform.getMachineDescription(); - ByteBuffer data = Buffers.newDirectByteBuffer( machine.pageAlignedSize(avail) ); + ByteBuffer data = Buffers.newDirectByteBuffer( machine.pageAlignedSize( initialCapacity ) ); byte[] chunk = new byte[machine.pageSizeInBytes()]; int chunk2Read = Math.min(machine.pageSizeInBytes(), avail); int numRead = 0; @@ -232,8 +247,8 @@ public class IOUtil { data.put(chunk, 0, numRead); } avail = stream.available(); - chunk2Read = Math.min(machine.pageSizeInBytes(), avail); - } while (avail > 0 && numRead >= 0); + chunk2Read = Math.min(machine.pageSizeInBytes(), avail); + } while ( numRead > -1 ); // EOS: -1 == numRead data.flip(); return data; |