Search in sources :

Example 51 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project rocketmq by apache.

the class UtilAll method uncompress.

public static byte[] uncompress(final byte[] src) throws IOException {
    byte[] result = src;
    byte[] uncompressData = new byte[src.length];
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(src);
    InflaterInputStream inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    try {
        while (true) {
            int len = inflaterInputStream.read(uncompressData, 0, uncompressData.length);
            if (len <= 0) {
                break;
            }
            byteArrayOutputStream.write(uncompressData, 0, len);
        }
        byteArrayOutputStream.flush();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        throw e;
    } finally {
        try {
            byteArrayInputStream.close();
        } catch (IOException e) {
            log.error("Failed to close the stream", e);
        }
        try {
            inflaterInputStream.close();
        } catch (IOException e) {
            log.error("Failed to close the stream", e);
        }
        try {
            byteArrayOutputStream.close();
        } catch (IOException e) {
            log.error("Failed to close the stream", e);
        }
    }
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 52 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project pac4j by pac4j.

the class RedirectSAML2ClientTests method getInflatedAuthnRequest.

private String getInflatedAuthnRequest(final String location) {
    final List<NameValuePair> pairs = URLEncodedUtils.parse(java.net.URI.create(location), "UTF-8");
    final Inflater inflater = new Inflater(true);
    final byte[] decodedRequest = Base64.getDecoder().decode(pairs.get(0).getValue());
    final ByteArrayInputStream is = new ByteArrayInputStream(decodedRequest);
    final InflaterInputStream inputStream = new InflaterInputStream(is, inflater);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
    String line;
    final StringBuilder bldr = new StringBuilder();
    try {
        while ((line = reader.readLine()) != null) {
            bldr.append(line);
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    return bldr.toString();
}
Also used : NameValuePair(org.apache.http.NameValuePair) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) BufferedReader(java.io.BufferedReader) Inflater(java.util.zip.Inflater) IOException(java.io.IOException)

Example 53 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project scout.rt by eclipse.

the class SoapServiceTunnelContentHandler method getData.

protected Object getData(String dataPart, boolean compressed) throws IOException, ClassNotFoundException {
    Inflater inflater = null;
    try {
        // decode serial data
        InputStream in = new ByteArrayInputStream(Base64Utility.decode(dataPart));
        if (compressed) {
            inflater = new Inflater();
            in = new InflaterInputStream(in, inflater);
        }
        return getObjectSerializer().deserialize(in, null);
    } finally {
        if (inflater != null) {
            try {
                inflater.end();
            } catch (Throwable fatal) {
            // NOSONAR
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 54 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project dataverse by IQSS.

the class FastGetRecord method harvestRecord.

public void harvestRecord(String baseURL, String identifier, String metadataPrefix) throws IOException, ParserConfigurationException, SAXException, TransformerException {
    xmlInputFactory = javax.xml.stream.XMLInputFactory.newInstance();
    String requestURL = getRequestURL(baseURL, identifier, metadataPrefix);
    InputStream in = null;
    URL url = new URL(requestURL);
    HttpURLConnection con = null;
    int responseCode = 0;
    con = (HttpURLConnection) url.openConnection();
    con.setRequestProperty("User-Agent", "DataverseHarvester/3.0");
    con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
    try {
        responseCode = con.getResponseCode();
    // logger.debug("responseCode=" + responseCode);
    } catch (FileNotFoundException e) {
        // logger.info(requestURL, e);
        responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
    }
    if (responseCode == 200) {
        String contentEncoding = con.getHeaderField("Content-Encoding");
        if ("compress".equals(contentEncoding)) {
            ZipInputStream zis = new ZipInputStream(con.getInputStream());
            zis.getNextEntry();
            in = zis;
        } else if ("gzip".equals(contentEncoding)) {
            in = new GZIPInputStream(con.getInputStream());
        } else if ("deflate".equals(contentEncoding)) {
            in = new InflaterInputStream(con.getInputStream());
        } else {
            in = con.getInputStream();
        }
        // We are going to read the OAI header and SAX-parse it for the
        // error messages and other protocol information;
        // The metadata section we're going to simply save in a temporary
        // file, unparsed.
        BufferedReader rd = new BufferedReader(new InputStreamReader(in));
        String line = null;
        String oaiResponseHeader = "";
        boolean metadataFlag = false;
        boolean metadataWritten = false;
        boolean schemaChecked = false;
        FileOutputStream tempFileStream = null;
        PrintWriter metadataOut = null;
        savedMetadataFile = File.createTempFile("meta", ".tmp");
        int mopen = 0;
        int mclose = 0;
        while ((line = rd.readLine()) != null) {
            if (!metadataFlag) {
                if (line.matches(".*" + XML_METADATA_TAG_OPEN + ".*")) {
                    String lineCopy = line;
                    int i = line.indexOf(XML_METADATA_TAG_OPEN);
                    if (line.length() > i + XML_METADATA_TAG_OPEN.length()) {
                        line = line.substring(i + XML_METADATA_TAG_OPEN.length());
                        // in the remaining part of the line?
                        if ((i = line.indexOf('<')) > -1) {
                            if (i > 0) {
                                line = line.substring(i);
                            }
                        } else {
                            line = null;
                        }
                    } else {
                        line = null;
                    }
                    oaiResponseHeader = oaiResponseHeader.concat(lineCopy.replaceAll(XML_METADATA_TAG_OPEN + ".*", XML_METADATA_TAG_OPEN + XML_METADATA_TAG_CLOSE + XML_OAI_PMH_CLOSING_TAGS));
                    tempFileStream = new FileOutputStream(savedMetadataFile);
                    metadataOut = new PrintWriter(tempFileStream, true);
                    // metadataOut.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); /* ? */
                    metadataFlag = true;
                } else if (line.matches(".*<" + XML_METADATA_TAG + " [^>]*>.*")) {
                    if (metadataPrefix.equals(DATAVERSE_EXTENDED_METADATA)) {
                        oaiResponseHeader = oaiResponseHeader.concat(line);
                        metadataWritten = true;
                        metadataFlag = true;
                    }
                }
            }
            if (line != null) {
                if (metadataFlag) {
                    if (!metadataWritten) {
                        if (line.matches("<" + XML_METADATA_TAG)) {
                            int i = 0;
                            while ((i = line.indexOf("<" + XML_METADATA_TAG, i)) > -1) {
                                if (!line.substring(i).matches("^<" + XML_METADATA_TAG + "[^>]*/")) {
                                    // don't count if it's a closed, empty tag:
                                    // <metadata />
                                    mopen++;
                                }
                                i += XML_METADATA_TAG_OPEN.length();
                            }
                        }
                        if (line.matches(".*" + XML_METADATA_TAG_CLOSE + ".*")) {
                            int i = 0;
                            while ((i = line.indexOf(XML_METADATA_TAG_CLOSE, i)) > -1) {
                                i += XML_METADATA_TAG_CLOSE.length();
                                mclose++;
                            }
                            if (mclose > mopen) {
                                line = line.substring(0, line.lastIndexOf(XML_METADATA_TAG_CLOSE));
                                metadataWritten = true;
                            }
                        }
                        if (!schemaChecked) {
                            // if the top-level XML element lacks the schema definition,
                            // insert the generic xmlns and xmlns:xsi attributes; these
                            // may be needed by the transform stylesheets.
                            // this mimicks the behaviour of the OCLC GetRecord
                            // client implementation.
                            // -L.A.
                            int offset = 0;
                            // However, there may be one or more XML comments before
                            // the first "real" XML element (of the form
                            // <!-- ... -->). So we need to skip these!
                            int j = 0;
                            while (((j = line.indexOf('<', offset)) > -1) && line.length() >= j + XML_COMMENT_START.length() && XML_COMMENT_START.equals(line.substring(j, j + XML_COMMENT_START.length()))) {
                                offset = j;
                                while (line != null && ((offset = line.indexOf(XML_COMMENT_END, offset)) < 0)) {
                                    line = line.replaceAll("[\n\r]", " ");
                                    offset = line.length();
                                    line = line.concat(rd.readLine());
                                }
                                offset += XML_COMMENT_END.length();
                            }
                            // if we have skipped some comments, is there another
                            // XML element left in the buffered line?
                            int firstElementStart = -1;
                            if ((firstElementStart = line.indexOf('<', offset)) > -1) {
                                // OK, looks like there is.
                                // is it terminated?
                                // if not, let's read the stream until
                                // we find the closing '>':
                                int firstElementEnd = -1;
                                offset = firstElementStart;
                                while (line != null && ((firstElementEnd = line.indexOf('>', offset)) < 0)) {
                                    line = line.replaceAll("[\n\r]", "");
                                    offset = line.length();
                                    line = line.concat(rd.readLine());
                                }
                                if (firstElementEnd < 0) {
                                    // this should not happen!
                                    // we've reached the end of the XML stream
                                    // without encountering a single valid XML tag -- ??
                                    this.errorMessage = "Malformed GetRecord response; reached the end of the stream but couldn't find a single valid XML element in the metadata section.";
                                } else {
                                    // OK, we now have a line that contains a complete,
                                    // terminated (possibly multi-line) first XML element
                                    // that starts at [offset].
                                    int i = firstElementStart;
                                    if (!line.substring(i).matches("^<[^>]*" + XML_XMLNS_XSI_ATTRIBUTE_TAG + ".*")) {
                                        String head = line.substring(0, i);
                                        String tail = line.substring(i);
                                        // tail = tail.replaceFirst(">", " xmlns=\"http://www.openarchives.org/OAI/2.0/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
                                        tail = tail.replaceFirst(">", XML_XMLNS_XSI_ATTRIBUTE);
                                        line = head + tail;
                                    }
                                    schemaChecked = true;
                                }
                            } else {
                            // there was no "real" XML elements, only comments.
                            // We'll perform this schema check in the next
                            // iteration.
                            }
                        }
                        metadataOut.println(line);
                    }
                } else {
                    oaiResponseHeader = oaiResponseHeader.concat(line);
                }
            }
        }
        // parse the OAI Record header:
        XMLStreamReader xmlr = null;
        try {
            StringReader reader = new StringReader(oaiResponseHeader);
            xmlr = xmlInputFactory.createXMLStreamReader(reader);
            processOAIheader(xmlr, metadataPrefix.equals(DATAVERSE_EXTENDED_METADATA));
        } catch (XMLStreamException ex) {
            // Logger.getLogger("global").log(Level.SEVERE, null, ex);
            if (this.errorMessage == null) {
                this.errorMessage = "Malformed GetRecord response: " + oaiResponseHeader;
            }
            // delete the temp metadata file; we won't need it:
            if (savedMetadataFile != null) {
            // savedMetadataFile.delete();
            }
        }
        try {
            if (xmlr != null) {
                xmlr.close();
            }
        } catch (Exception ed) {
        // seems OK to ignore;
        }
        if (rd != null) {
            rd.close();
        }
        if (metadataOut != null) {
            metadataOut.close();
        }
        if (!(metadataWritten) && !(this.isDeleted())) {
            if (oaiResponseHeader.length() > 64) {
                oaiResponseHeader = oaiResponseHeader.substring(0, 32) + "...";
            }
            this.errorMessage = "Failed to parse GetRecord response; " + oaiResponseHeader;
        // savedMetadataFile.delete();
        }
        if (this.isDeleted()) {
        // savedMetadataFile.delete();
        }
    } else {
        this.errorMessage = "GetRecord request failed. HTTP error code " + responseCode;
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) TransformerException(javax.xml.transform.TransformerException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) HttpURLConnection(java.net.HttpURLConnection) XMLStreamException(javax.xml.stream.XMLStreamException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) PrintWriter(java.io.PrintWriter)

Example 55 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project Nukkit by Nukkit.

the class ZlibSingleThreadLowMem method inflate.

@Override
public synchronized byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
    byte[] bufferOut;
    int length;
    try {
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        outputStream.flush();
        bufferOut = outputStream.toByteArray();
        outputStream.close();
        inputStream.close();
    }
    return bufferOut;
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)200 ByteArrayInputStream (java.io.ByteArrayInputStream)113 InputStream (java.io.InputStream)100 IOException (java.io.IOException)74 Inflater (java.util.zip.Inflater)66 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 GZIPInputStream (java.util.zip.GZIPInputStream)39 DataInputStream (java.io.DataInputStream)33 FileInputStream (java.io.FileInputStream)27 BufferedInputStream (java.io.BufferedInputStream)24 InputStreamReader (java.io.InputStreamReader)13 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)13 HttpURLConnection (java.net.HttpURLConnection)12 URL (java.net.URL)11 File (java.io.File)10 BufferedReader (java.io.BufferedReader)9 OutputStream (java.io.OutputStream)9 Point (java.awt.Point)7 EOFException (java.io.EOFException)7 URLConnection (java.net.URLConnection)7