Easy way to write contents of a Java InputStream to an OutputStream

后端 未结 23 2480
粉色の甜心
粉色の甜心 2020-11-22 02:10

I was surprised to find today that I couldn\'t track down any simple way to write the contents of an InputStream to an OutputStream in Java. Obviou

相关标签:
23条回答
  • 2020-11-22 02:45
    public static boolean copyFile(InputStream inputStream, OutputStream out) {
        byte buf[] = new byte[1024];
        int len;
        long startTime=System.currentTimeMillis();
    
        try {
            while ((len = inputStream.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
    
            long endTime=System.currentTimeMillis()-startTime;
            Log.v("","Time taken to transfer all bytes is : "+endTime);
            out.close();
            inputStream.close();
    
        } catch (IOException e) {
    
            return false;
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-22 02:46

    As WMR mentioned, org.apache.commons.io.IOUtils from Apache has a method called copy(InputStream,OutputStream) which does exactly what you're looking for.

    So, you have:

    InputStream in;
    OutputStream out;
    IOUtils.copy(in,out);
    in.close();
    out.close();
    

    ...in your code.

    Is there a reason you're avoiding IOUtils?

    0 讨论(0)
  • 2020-11-22 02:46

    This is my best shot!!

    And do not use inputStream.transferTo(...) because is too generic. Your code performance will be better if you control your buffer memory.

    public static void transfer(InputStream in, OutputStream out, int buffer) throws IOException {
        byte[] read = new byte[buffer]; // Your buffer size.
        while (0 < (buffer = in.read(read)))
            out.write(read, 0, buffer);
    }
    

    I use it with this (improvable) method when I know in advance the size of the stream.

    public static void transfer(int size, InputStream in, OutputStream out) throws IOException {
        transfer(in, out,
                size > 0xFFFF ? 0xFFFF // 16bits 65,536
                        : size > 0xFFF ? 0xFFF// 12bits 4096
                                : size < 0xFF ? 0xFF // 8bits 256
                                        : size
        );
    }
    
    0 讨论(0)
  • 2020-11-22 02:47

    I use BufferedInputStream and BufferedOutputStream to remove the buffering semantics from the code

    try (OutputStream out = new BufferedOutputStream(...);
         InputStream in   = new BufferedInputStream(...))) {
      int ch;
      while ((ch = in.read()) != -1) {
        out.write(ch);
      }
    }
    
    0 讨论(0)
  • 2020-11-22 02:47

    you can use this method

    public static void copyStream(InputStream is, OutputStream os)
     {
         final int buffer_size=1024;
         try
         {
             byte[] bytes=new byte[buffer_size];
             for(;;)
             {
               int count=is.read(bytes, 0, buffer_size);
               if(count==-1)
                   break;
               os.write(bytes, 0, count);
             }
         }
         catch(Exception ex){}
     }
    
    0 讨论(0)
  • 2020-11-22 02:49

    I think this will work, but make sure to test it... minor "improvement", but it might be a bit of a cost at readability.

    byte[] buffer = new byte[1024];
    int len;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    
    0 讨论(0)
提交回复
热议问题