Java Convert via ByteBuffer toBytes(InputStream input)

Here you can find the source of toBytes(InputStream input)

Description

Converts an Input Stream to byte[]

License

Open Source License

Parameter

Parameter Description
the input stream

Exception

Parameter Description
IOException an exception

Return

the byte[] read from the input stream

Declaration

public static byte[] toBytes(InputStream input) throws IOException 

Method Source Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;

public class Main {
    private static final int EOF = -1;
    public static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
    public static final Charset utf8Charset = Charset.forName("UTF-8");

    /**/*from   ww w  . j a  v a 2  s.  co  m*/
     * Converts an Input Stream to byte[]
     * @param the input stream
     * @return the byte[] read from the input stream
     * @throws IOException
     */
    public static byte[] toBytes(InputStream input) throws IOException {
        if (input == null)
            return null;
        try {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int n = 0;
            int count = -1;
            while (EOF != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
                count = ((count == -1) ? n : (count + n));
            }
            output.flush();
            if (count == -1)
                return null;
            else
                return output.toByteArray();
        } finally {
            if (input != null)
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    public static byte[] toBytes(String value, CodingErrorAction codingErrorAction) {
        if (value != null) {
            try {
                if (codingErrorAction == null)
                    codingErrorAction = CodingErrorAction.IGNORE;
                final CharsetEncoder encoder = utf8Encoder(codingErrorAction);
                final ByteBuffer b = encoder.encode(CharBuffer.wrap(value));
                final byte[] bytes = new byte[b.remaining()];
                b.get(bytes);
                return bytes;
            } catch (Throwable t) {
                t.printStackTrace();
                return value.getBytes(utf8Charset);
            }
        }
        return null;
    }

    public static CharsetEncoder utf8Encoder(CodingErrorAction codingErrorAction) {
        try {
            if (codingErrorAction == null)
                codingErrorAction = CodingErrorAction.REPORT;
            final CharsetEncoder encoder = utf8Charset.newEncoder();
            encoder.reset();
            encoder.onUnmappableCharacter(codingErrorAction);
            encoder.onMalformedInput(codingErrorAction);
            return encoder;
        } catch (Throwable t) {
            t.printStackTrace();
            return null;
        }
    }
}

Related

  1. toBytes(char[] ch)
  2. toBytes(char[] chars)
  3. toBytes(char[] string)
  4. toBytes(final float val)
  5. toBytes(final UUID uuid)
  6. toBytes(int value)
  7. toBytes(long l)
  8. toBytes(Number value)
  9. toBytes(Object obj)