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;
}
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();
}
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
}
}
}
}
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;
}
}
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;
}
Aggregations