You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Sebb (JIRA)" <ji...@apache.org> on 2013/05/30 02:18:20 UTC

[jira] [Commented] (LANG-897) IOUtils: add support for copying from large byte buffers

    [ https://issues.apache.org/jira/browse/LANG-897?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13669912#comment-13669912 ] 

Sebb commented on LANG-897:
---------------------------

An alternative would be to create ChunkedOutputStream and ChunkedWriter classes.
                
> IOUtils: add support for copying from large byte buffers
> --------------------------------------------------------
>
>                 Key: LANG-897
>                 URL: https://issues.apache.org/jira/browse/LANG-897
>             Project: Commons Lang
>          Issue Type: New Feature
>    Affects Versions: 3.1
>            Reporter: Sebb
>
> Trying to write a large byte array to a FileOutputStream may cause OOME.
> This is because such output requires the use of native code, and native code may need to copy the array in order to access it safely, see:
> http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp1265
> It might therefore be useful to have a method which writes from the byte array in chunks. One can create a ByteArrayInputStream from the input, and then use one of the copy() methods, but that creates an unnecessary array buffer (albeit only 4k).
> There are already write methods which copy byte[] to OutputStream and char[] to Writer.
> Some or all of these could be converted to use chunked output, or there could be new methods to implement the chunking.
> Here is a sample implementation of a stand-alone method:
> {code}
> public static void writeChunked(byte[] data, OutputStream output) throws IOException {
>     int bytes = data.length;
>     int offset = 0;
>     while(bytes > 0) {
>         int chunk = Math.min(bytes, DEFAULT_BUFFER_SIZE);
>         output.write(data, offset, chunk);
>         bytes -= chunk;
>         offset += chunk;
>     }
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira