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:com.siemens.sw360.exporter.CSVExport.java

@NotNull
public static ByteArrayInputStream createCSV(Iterable<String> csvHeaderIterable,
        Iterable<Iterable<String>> inputIterable) throws IOException {
    final ByteArrayOutputStream outB = getCSVOutputStream(csvHeaderIterable, inputIterable);

    return new ByteArrayInputStream(outB.toByteArray());
}

From source file:Main.java

public static int byteArrayToInt(byte[] input) {
    try {//from   www.j av a  2 s .co m
        ByteArrayInputStream bis = new ByteArrayInputStream(input);
        DataInputStream dis = new DataInputStream(bis);
        int numb = dis.readInt();
        dis.close();
        return numb;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.es.nsi.topology.translator.http.Decoder.java

private static byte[] decompress(byte[] source) throws IOException {
    try (ByteArrayInputStream is = new ByteArrayInputStream(source)) {
        return gunzip(is);
    } catch (IOException io) {
        log.error("Failed to decompress document", io);
        throw io;
    }//  ww  w. j a  va  2 s .c om
}

From source file:Main.java

@Nullable
public static String decompress(String input) {
    StringBuilder sb = new StringBuilder();
    ByteArrayInputStream is = null;
    GZIPInputStream gis = null;//from  w w  w .j  a va 2 s. c o m
    try {
        final byte[] bytes = Base64.decode(input, Base64.DEFAULT);
        is = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(is, BUFFER_SIZE);

        int cache;
        final byte[] data = new byte[BUFFER_SIZE];
        while ((cache = gis.read(data)) != -1) {
            sb.append(new String(data, 0, cache));
        }
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (gis != null)
                gis.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:Main.java

public static byte[] gzipDecompress(byte[] input) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//from   ww w  .  j a v a  2s  . c  om
        GZIPInputStream is = new GZIPInputStream(new ByteArrayInputStream(input));
        copy(is, out);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return out.toByteArray();
}

From source file:Main.java

public static Document stringToDocument(String assumedXml)
        throws ParserConfigurationException, SAXException, IOException {
    Document doc = null;//  w w  w .  j a va  2s  .com
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    doc = builder.parse(new InputSource(new ByteArrayInputStream(assumedXml.getBytes())));

    return doc;
}

From source file:io.wcm.caravan.io.jsontransform.JsonTestHelper.java

public static Source fromJson(String json) throws JsonParseException, IOException {
    return new JacksonStreamSource(new ByteArrayInputStream(json.getBytes()));
}

From source file:Main.java

/**
 * Redintegrate a serialized object./*from  w w w. jav a  2 s  .com*/
 * @param serial a byte array of the serialized object.
 * @return the original object or null if an error occurs.
 */
public static Object deserialize(byte[] serial) {
    ByteArrayInputStream bais = new ByteArrayInputStream(serial);
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } catch (IOException e) {
        return null;
    } catch (ClassNotFoundException e) {
        return null;
    } finally {
        try {
            if (ois != null)
                ois.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

protected static Object fromBase64(String base64String) {
    try {/*from w  ww .j  a v a2 s . c o m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(base64String));

        try {
            ZipInputStream zip = new ZipInputStream(bis);
            try {
                zip.getNextEntry();
                ObjectInput in = new ObjectInputStream(zip);
                try {
                    return in.readObject();
                } finally {
                    in.close();
                }
            } finally {
                zip.close();
            }
        } finally {
            bis.close();
        }
    } catch (ClassNotFoundException e) {
        log.log((Level.SEVERE), "ClassNotFoundException while deserializing member", e);
        return null;
    } catch (IOException e) {
        log.log((Level.SEVERE), "IOException while deserializing member", e);
        return null;
    }
}

From source file:Main.java

public static long write(String fileName, String data, int position) throws FileNotFoundException, IOException {
    boolean append = false;
    if (position > 0) {
        truncateFile(fileName, position);
        append = true;/*from   www .j a v  a 2 s.  c om*/
    }

    byte[] rawData = data.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    FileOutputStream out = new FileOutputStream(fileName, append);
    byte buff[] = new byte[rawData.length];
    in.read(buff, 0, buff.length);
    out.write(buff, 0, rawData.length);
    out.flush();
    out.close();

    return data.length();
}