Example usage for java.io ByteArrayInputStream ByteArrayInputStream

List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream ByteArrayInputStream.

Prototype

public ByteArrayInputStream(byte buf[]) 

Source Link

Document

Creates a ByteArrayInputStream so that it uses buf as its buffer array.

Usage

From source file:org.energyos.espi.common.test.FixtureFactory.java

public static InputStream newInputStream(String fileName) throws IOException {
    return new ByteArrayInputStream(newXML(fileName).getBytes());
}

From source file:Main.java

public static byte[] decrypt(byte[] byteArray, PublicKey publicKey) {
    Cipher cipher = null;//  w  w w .  jav  a  2s . co m

    try {
        cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {

        cipher.init(Cipher.DECRYPT_MODE, publicKey);

    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try {

        while (input.available() != 0) {
            byte[] t0 = new byte[128];
            input.read(t0);
            output.write(cipher.doFinal(t0));
        }

    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toByteArray();
}

From source file:BytesUtil.java

public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
    Object obj = null;/* w  w w  . jav  a 2  s.co m*/
    ByteArrayInputStream bis = null;
    ObjectInputStream ois = null;
    try {
        bis = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bis);
        obj = ois.readObject();
    } finally {
        if (bis != null) {
            bis.close();
        }
        if (ois != null) {
            ois.close();
        }
    }
    return obj;
}

From source file:com.storageroomapp.client.util.FileUtil.java

static public boolean writeStringToFile(File fileToWrite, String text) {
    InputStream is = new ByteArrayInputStream(text.getBytes());
    return writeStreamToFile(fileToWrite, is, true);
}

From source file:Main.java

public static InputStream toInputStream(CharSequence input) {
    return new ByteArrayInputStream(input.toString().getBytes());
}

From source file:Main.java

public static Document getDocument(String xmlString) {
    Document document = null;/*from   w w w .j  av  a  2 s .c  o m*/
    try {
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        document = builder.parse(new ByteArrayInputStream(xmlString.getBytes()));
        document.getDocumentElement().normalize();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return document;
}

From source file:Main.java

/**
 * Returns a object from the given byte array.
 * // w  ww .j a  v a 2 s.  co  m
 * @param bytes
 *            array to convert
 * @return object
        
 */
public static Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream is = new ObjectInputStream(bais);
    return is.readObject();
}

From source file:com.jaspersoft.jasperserver.util.JasperSerializationUtil.java

/**
 * DeSerialize a byte array//from   w w  w.  j  a v  a 2  s  . com
 * @param input
 * @return
 */
public static Object deserialize(byte[] input) {

    Object obj = null;
    Exception exp = null;
    ByteArrayInputStream bais = new ByteArrayInputStream(input);
    JasperObjectInputStream jois = null;
    long startTime = System.currentTimeMillis();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Enter deserialize .. Start Time" + System.currentTimeMillis());
        }

        jois = new JasperSerializationUtil().new JasperObjectInputStream(bais);
        obj = jois.readObject();
    } catch (IOException e) {
        exp = e;
    } catch (ClassNotFoundException e) {
        exp = e;
    } finally {
        try {
            if (null != jois) {
                jois.close();
            }
            bais.close();
        } catch (IOException e1) {
        }
        if (logger.isDebugEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            logger.debug("Exit deserialize .. Total Time Spent: " + elapsedTime);
        }

        if (null != exp) {
            logger.error(exp.getMessage(), exp);
            throw new RuntimeException(exp);
        }
    }
    return obj;
}

From source file:Main.java

/**
 * Wraps a ByteBuffer in an InputStream. 
 * //  ww  w  .j a va  2  s  .  c o  m
 * @param byteBuffer The ByteBuffer to wrap.
 * 
 * @return An InputStream wrapping the ByteBuffer content.
 */
public static InputStream toStream(ByteBuffer byteBuffer) {
    byte[] bytes = new byte[byteBuffer.remaining()];
    byteBuffer.get(bytes);
    return new ByteArrayInputStream(bytes);
}

From source file:net.orpiske.tcs.utils.compression.Decompressor.java

/**
 * Decompress an array of bytes/*from   w w  w  .ja  va  2 s  .  c o m*/
 * @param bytes the array to decompress
 * @return a String object with the text
 * @throws IOException if unable to decompress it for any reason
 */
public static String decompress(final byte[] bytes) throws IOException {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    InputStream gzipInputStream = new GZIPInputStream(inputStream);

    /**
     * Ok, this should be "smarter". Will fix this, eventually ...
     */
    Reader reader = new InputStreamReader(gzipInputStream, Charset.forName("UTF-8"));

    BufferedReader bufferedReader = new BufferedReader(reader);
    StringBuilder builder = new StringBuilder();

    try {
        char[] buffer = new char[1];

        while (bufferedReader.read(buffer) > 0) {
            builder.append(buffer);
        }

        return builder.toString();
    } finally {
        IOUtils.closeQuietly(gzipInputStream);
        IOUtils.closeQuietly(inputStream);
    }
}