Skip to main content
Edit by Boris Schegolev was not correct: the code will not compile, since OutputStream does not have toByteArray method.
Source Link
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    OutputStreamByteArrayOutputStream os = new ByteArrayOutputStream();  
    byte[] buffer = new byte[0xFFFF];
    for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    OutputStream os = new ByteArrayOutputStream();  
    byte[] buffer = new byte[0xFFFF];
    for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    byte[] buffer = new byte[0xFFFF];
    for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
improved formatting to follow common IDE conventions
Source Link
Boris Schegolev
  • 3.7k
  • 5
  • 23
  • 35
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    ByteArrayOutputStreamOutputStream os = new ByteArrayOutputStream();  
    byte[] buffer = new byte[0xFFFF];
    for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    byte[] buffer = new byte[0xFFFF];
    for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    OutputStream os = new ByteArrayOutputStream();  
    byte[] buffer = new byte[0xFFFF];
    for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
fixed the code to make it working, improved formatting to follow common IDE conventions
Source Link
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    OutputStreamByteArrayOutputStream os = new ByteArrayOutputStream();
        
    byte[] buffer = new byte[0xFFFF];
    for (int len=islen = is.read(buffer);len; len != -1;len=is1; len = is.read(buffer))   { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    OutputStream os = new ByteArrayOutputStream();
        
    byte[] buffer = new byte[0xFFFF];
    for(int len=is.read(buffer);len!=-1;len=is.read(buffer))    
        os.write(buffer, 0, len);

    return os.toByteArray();
}
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    byte[] buffer = new byte[0xFFFF];
    for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { 
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}
simplify implementation
Source Link
sigpwned
  • 7.5k
  • 6
  • 32
  • 54
Loading
changed code to use try-with-resources, but keep throwing io-exception.
Source Link
kritzikratzi
  • 20.3k
  • 1
  • 31
  • 40
Loading
Source Link
oliverkn
  • 743
  • 9
  • 15
Loading