Java BufferedImage to Byte Array getBytes(final InputStream is, final int bufferSize)

Here you can find the source of getBytes(final InputStream is, final int bufferSize)

Description

get Bytes

License

LGPL

Declaration

public static byte[] getBytes(final InputStream is, final int bufferSize) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.lang.reflect.Array;

public class Main {
    public static byte[] getBytes(final BufferedImage image) throws IOException {
        return getBytes(image, "jpg");
    }/*from  w w  w . jav a  2  s  .  co  m*/

    public static byte[] getBytes(final BufferedImage image, final String format) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, null != format ? format : "jpg", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();
        return imageInByte;
    }

    public static byte[] getBytes(final InputStream is) throws IOException {
        return getBytes(is, 1024);
    }

    public static byte[] getBytes(final InputStream is, final int bufferSize) throws IOException {
        final ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize);
        try {
            byte[] buffer = new byte[bufferSize];
            int len;

            while ((len = is.read(buffer)) >= 0) {
                out.write(buffer, 0, len);
            }
        } finally {
            out.close();
        }
        return out.toByteArray();
    }

    public static byte[] getBytes(final Object data) throws IOException {
        byte[] result = new byte[0];
        if (!isByteArray(data)) {
            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutput out = null;
            try {
                out = new ObjectOutputStream(bos);
                out.writeObject(data);
                result = bos.toByteArray();
            } finally {
                if (null != out)
                    out.close();
                bos.close();
            }
        } else {
            result = (byte[]) data;
        }
        return result;
    }

    public static boolean isByteArray(final Object data) {
        if (data.getClass().isArray()) {
            final Object val = Array.get(data, 0);
            if (val instanceof Byte) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. getByteArray(RenderedImage image, String formatName)
  2. getByteBuffer(BufferedImage bufferedImage)
  3. getByteBufferToImage(ByteBuffer buffer, int width, int height)
  4. getByteImage(BufferedImage imagem)
  5. getBytes(BufferedImage image)
  6. getBytesFromBufferedImage(BufferedImage bi, String format)
  7. getBytesFromImage(String fileIn, String fileOut)
  8. getBytesPerPixel(int bufferedImageType)
  9. image2Bytes(BufferedImage image)