Example usage for java.io ByteArrayInputStream close

List of usage examples for java.io ByteArrayInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayInputStream has no effect.

Usage

From source file:Main.java

public static byte[] unZip(byte[] bContent) {
    byte[] b = null;
    try {//from   w  ww . j a v  a  2 s.  com
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);
        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:bencoding.securely.Converters.java

public static Object deserializeObjectFromString(String objectString) throws Exception {

    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
            Base64.decode(objectString, Base64.DEFAULT));
    GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
    ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);

    Object object = objectInputStream.readObject();

    objectInputStream.close();/*from  ww  w  . j  av  a2 s  . c  o m*/
    gzipInputStream.close();
    arrayInputStream.close();

    return object;
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) throws IOException {
    byte[] b = null;
    try {// w w w .j av a 2  s  . c o  m
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);

        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:com.crawlersick.nettool.GetattchmentFromMail1.java

public static String decompress(byte[] compressed) throws IOException {
    final int BUFFER_SIZE = 32;
    ByteArrayInputStream is = new ByteArrayInputStream(compressed);
    GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
    StringBuilder string = new StringBuilder();
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = gis.read(data)) != -1) {
        string.append(new String(data, 0, bytesRead));
    }/*from w ww  . j a  v a 2s  . c  om*/
    gis.close();
    is.close();
    return string.toString();
}

From source file:fr.paris.lutece.plugins.calendar.modules.document.service.search.DocumentCalendarIndexer.java

/**
 * Get the content from the document/*  www . ja v a 2s  .com*/
 * @param document the document to index
 * @return the content
 */
private static String getContentToIndex(fr.paris.lutece.plugins.document.business.Document document) {
    StringBuffer sbContentToIndex = new StringBuffer();
    sbContentToIndex.append(document.getTitle());

    for (DocumentAttribute attribute : document.getAttributes()) {
        if (attribute.isSearchable()) {
            if (!attribute.isBinary()) {
                // Text attributes
                sbContentToIndex.append(attribute.getTextValue());
                sbContentToIndex.append(" ");
            } else {
                // Binary file attribute
                // Gets indexer depending on the ContentType (ie: "application/pdf" should use a PDF indexer)
                IFileIndexer indexer = _factoryIndexer.getIndexer(attribute.getValueContentType());

                if (indexer != null) {
                    try {
                        ByteArrayInputStream bais = new ByteArrayInputStream(attribute.getBinaryValue());
                        sbContentToIndex.append(indexer.getContentToIndex(bais));
                        sbContentToIndex.append(" ");
                        bais.close();
                    } catch (IOException e) {
                        AppLogService.error(e.getMessage(), e);
                    }
                }
            }
        }
    }

    // Index Metadata
    sbContentToIndex.append(document.getXmlMetadata());

    return sbContentToIndex.toString();
}

From source file:org.dasein.cloud.terremark.TerremarkMethod.java

private static ParsedError parseErrorResponse(String responseBody) throws CloudException, InternalException {
    try {//from  ww w.j a  v  a2s .  c o m
        ByteArrayInputStream bas = new ByteArrayInputStream(responseBody.getBytes());

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document doc = parser.parse(bas);

        bas.close();

        Node errorNode = doc.getElementsByTagName(ERROR_TAG).item(0);
        ParsedError error = new ParsedError();
        if (errorNode != null) {
            error.message = errorNode.getAttributes().getNamedItem(MESSAGE_ATTR).getNodeValue();
            error.code = Integer
                    .parseInt(errorNode.getAttributes().getNamedItem(MAJOR_CODE_ATTR).getNodeValue());
        }

        return error;
    } catch (IOException e) {
        throw new CloudException(e);
    } catch (ParserConfigurationException e) {
        throw new CloudException(e);
    } catch (SAXException e) {
        throw new CloudException(e);
    }
}

From source file:org.dasein.cloud.terremark.TerremarkMethod.java

private static Document parseResponse(String responseBody) throws CloudException, InternalException {
    try {/*from   w w  w  .j  av a2s  .  c om*/
        if (wire.isDebugEnabled()) {
            String[] lines = responseBody.split("\n");

            if (lines.length < 1) {
                lines = new String[] { responseBody };
            }
            for (String l : lines) {
                wire.debug(l);
            }
        }
        ByteArrayInputStream bas = new ByteArrayInputStream(responseBody.getBytes());

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document doc = parser.parse(bas);

        bas.close();
        return doc;
    } catch (IOException e) {
        throw new CloudException(e);
    } catch (ParserConfigurationException e) {
        throw new CloudException(e);
    } catch (SAXException e) {
        throw new CloudException(e);
    }
}

From source file:org.nuxeo.theme.presets.PhotoshopPaletteParser.java

public static Map<String, String> parse(byte[] bytes) {
    Map<String, String> entries = new LinkedHashMap<String, String>();
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    DataInputStream dis = new DataInputStream(is);

    char[] words = new char[bytes.length];
    int size = 0;
    while (true) {
        try {//from w ww.  java2s  .  c  o  m
            words[size] = dis.readChar();
            size++;
        } catch (Exception e) {
            break;
        }
    }

    try {
        is.close();
        dis.close();
    } catch (IOException e) {
        log.error(e, e);
    }

    int offset = 1;
    int version = words[0] & 0xffff;
    int nc = words[1] & 0xffff;

    // get version 2 if it exists
    if (version == 1 && size > nc * 5 + 2) {
        offset += nc * 5 + 2;
        version = words[offset - 1] & 0xffff;
        nc = words[offset] & 0xffff;
    }

    if (version == 1) {
        log.debug("Found ACO v1 color file (Photoshop < 7.0)");
    } else if (version == 2) {
        log.debug("Found ACO v2 color file (Photoshop >= 7.0)");
    } else {
        log.error("Unknown ACO file version: " + version);
        return entries;
    }

    log.debug("Found " + nc + " colors.");

    int counter = 1;
    for (int j = 0; j < nc; j++) {
        String value = null;
        int colorSpace = words[offset + 1] & 0xff;
        int w = words[offset + 2] & 0xffff;
        int x = words[offset + 3] & 0xffff;
        int y = words[offset + 4] & 0xffff;
        int z = words[offset + 5] & 0xffff;

        if (colorSpace == RGB) {
            value = rgbToHex(w / 256, x / 256, y / 256);

        } else if (colorSpace == HSB) {
            float hue = w / 65535F; // [0.0-1.0]
            float saturation = x / 65535F; // [0.0-1.0]
            float brightness = y / 65535F; // [0.0-1.0]
            Color color = Color.getHSBColor(hue, saturation, brightness);
            value = rgbToHex(color.getRed(), color.getGreen(), color.getBlue());

        } else if (colorSpace == CMYK) {
            float cyan = 1F - w / 65535F; // [0.0-1.0]
            float magenta = 1F - x / 65535F; // [0.0-1.0]
            float yellow = 1F - y / 65535F; // [0.0-1.0]
            float black = 1F - z / 65535F; // [0.0-1.0]
            // TODO: do the conversion to RGB. An ICC profile is required.
            log.warn("Unsupported color space: CMYK");

        } else if (colorSpace == GRAYSCALE) {
            int gray = (int) (w * 256F / 10000F); // [0-256]
            value = rgbToHex(gray, gray, gray);

        } else if (colorSpace == LAB) {
            float l = w / 100F;
            float a = x / 100F;
            float b = y / 100F;
            // TODO: do the conversion to RGB. An ICC profile is required.
            log.warn("Unsupported color space: CIE Lab");

        } else if (colorSpace == WIDE_CMYK) {
            float cyan = w / 10000F; // [0.0-1.0]
            float magenta = x / 10000F; // [0.0-1.0]
            float yellow = y / 10000F; // [0.0-1.0]
            float black = z / 10000F; // [0.0-1.0]
            // TODO: do the conversion to RGB. An ICC profile is required.
            log.warn("Unsupported color space: Wide CMYK");

        } else {
            log.warn("Unknown color space: " + colorSpace);
        }

        String name = "";
        if (version == 1) {
            name = String.format("Color %s", counter);
        }

        else if (version == 2) {
            int len = (words[offset + 7] & 0xffff) - 1;
            name = String.copyValueOf(words, offset + 8, len);
            offset += len + 3;

            String n = name;
            int c = 2;
            while (entries.containsKey(n)) {
                n = String.format("%s %s", name, c);
                c++;
            }
            name = n;
        }

        if (value != null) {
            entries.put(name, value);
        }

        offset += 5;
        counter++;
    }

    return entries;
}

From source file:com.clustercontrol.plugin.impl.AsyncTask.java

/**
 * ??BinarySerializable???/*from   w  w w  . ja va  2s . co m*/
 * @param bytes ??Binary
 * @return Serializable
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static Serializable decodeBinary(byte[] bytes) throws IOException, ClassNotFoundException {
    ByteArrayInputStream iaos = null;
    ObjectInputStream ois = null;
    Object obj = null;

    try {
        iaos = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(iaos);

        obj = ois.readObject();
    } finally {
        if (iaos != null) {
            iaos.close();
        }
        if (ois != null) {
            ois.close();
        }
    }

    return (Serializable) obj;
}

From source file:com.glaf.core.util.FileUtils.java

public static void save(String filename, byte[] bytes) {
    ByteArrayInputStream bais = null;
    try {/*from   ww w. j a va  2s .  c o  m*/
        bais = new ByteArrayInputStream(bytes);
        save(filename, bais);
        bais.close();
        bais = null;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (bais != null) {
                bais.close();
            }
        } catch (IOException ex) {
        }
    }
}