Here you can find the source of getBytes(InputStream in)
Parameter | Description |
---|---|
in | - The InputStream to read from |
Parameter | Description |
---|---|
IOException | if an I/O error occurs |
public static byte[] getBytes(InputStream in) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { /**/*ww w. j a v a2 s .co m*/ * Given the supplied InputStream read all the bytes from it and return them as a * byte array. This does not close the supplied stream. * * @param in - The InputStream to read from * @throws IOException if an I/O error occurs */ public static byte[] getBytes(InputStream in) throws IOException { return getBytes(in, false); } /** * Given the supplied InputStream read all the bytes from it and return them as a * byte array. This closes the supplied stream if specified. * * @param in - The InputStream to read from * @param close - Close the stream when finished. * @throws IOException if an I/O error occurs */ public static byte[] getBytes(InputStream in, boolean close) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[2048]; int len; while ((len = in.read(buf)) != -1) { baos.write(buf, 0, len); } if (close) close(in); return baos.toByteArray(); } /** * Unconditionally close an <code>OutputStream</code>. * <p> * Equivalent to {@link java.io.OutputStream#close()}, except any exceptions will be * ignored. * * @param output - A (possibly null) OutputStream */ public static boolean close(OutputStream output) { if (null == output) return false; try { output.close(); return true; } catch (IOException ioe) { return false; } } /** * Unconditionally close an <code>InputStream</code>. * <p> * Equivalent to {@link java.io.InputStream#close()}, except any exceptions will be * ignored. * * @param input - A (possibly null) InputStream */ public static boolean close(InputStream input) { if (null == input) return false; try { input.close(); return true; } catch (IOException ioe) { return false; } } /** * Unconditionally close an <code>InputStream</code>/<code>OutputStream</code> pair. * * @param in - A (possibly null) InputStream * @param out - A (possibly null) OutputStream */ public static void close(InputStream in, OutputStream out) { try { if (in != null) in.close(); } catch (Exception ignored) { } try { if (out != null) out.close(); } catch (Exception ignored) { } } /** * Unconditionally close an <code>Writer</code>. * <p> * Equivalent to {@link java.io.Writer#close()}, except any exceptions will be * ignored. * * @param output - A (possibly null) Writer */ public static boolean close(Writer output) { if (null == output) return false; try { output.close(); return true; } catch (IOException ioe) { return false; } } /** * Unconditionally close an <code>Reader</code>. * <p> * Equivalent to {@link java.io.Reader#close()}, except any exceptions will be * ignored. * * @param input - A (possibly null) Reader */ public static boolean close(Reader input) { if (null == input) return false; try { input.close(); return true; } catch (IOException ioe) { return false; } } /** * Unconditionally close a <code>Reader</code>/<code>Writer</code> pair. * * @param in - A (possibly null) Reader * @param out - A (possibly null) Writer */ public static void close(Reader in, Writer out) { try { if (in != null) in.close(); } catch (Exception ignored) { } try { if (out != null) out.close(); } catch (Exception ignored) { } } }