use of java.io.PushbackInputStream in project j2objc by google.
the class ZipInputStream method closeEntry.
/**
* Closes the current zip entry and prepares to read the next entry.
*
* @throws IOException
* if an {@code IOException} occurs.
*/
public void closeEntry() throws IOException {
checkClosed();
if (currentEntry == null) {
return;
}
/*
* The following code is careful to leave the ZipInputStream in a
* consistent state, even when close() results in an exception. It does
* so by:
* - pushing bytes back into the source stream
* - reading a data descriptor footer from the source stream
* - resetting fields that manage the entry being closed
*/
// Ensure all entry bytes are read
Exception failure = null;
try {
Streams.skipAll(this);
} catch (Exception e) {
failure = e;
}
int inB, out;
if (currentEntry.compressionMethod == ZipEntry.DEFLATED) {
inB = inf.getTotalIn();
out = inf.getTotalOut();
} else {
inB = inRead;
out = inRead;
}
int diff = entryIn - inB;
// Pushback any required bytes
if (diff != 0) {
((PushbackInputStream) in).unread(buf, len - diff, diff);
}
try {
readAndVerifyDataDescriptor(inB, out, currentEntryIsZip64);
} catch (Exception e) {
if (failure == null) {
// otherwise we're already going to throw
failure = e;
}
}
inf.reset();
lastRead = inRead = entryIn = len = 0;
crc.reset();
currentEntry = null;
if (failure != null) {
if (failure instanceof IOException) {
throw (IOException) failure;
} else if (failure instanceof RuntimeException) {
throw (RuntimeException) failure;
}
AssertionError error = new AssertionError();
error.initCause(failure);
throw error;
}
}
use of java.io.PushbackInputStream in project j2objc by google.
the class GZIPInputStream method maybeReadNextMember.
private boolean maybeReadNextMember() throws IOException {
// If we have any unconsumed data in the inflater buffer, we have to
// scan that first. The fact that we've reached here implies we've
// successfully consumed the GZIP trailer.
final int remaining = inf.getRemaining() - GZIP_TRAILER_SIZE;
if (remaining > 0) {
// remaining when it is first created.
if (!(in instanceof PushbackInputStream)) {
in = new PushbackInputStream(in, buf.length);
}
((PushbackInputStream) in).unread(buf, inf.getCurrentOffset() + GZIP_TRAILER_SIZE, remaining);
}
final byte[] buffer;
try {
buffer = readHeader(in);
} catch (EOFException eof) {
// gzip record.
return true;
}
final short magic = Memory.peekShort(buffer, 0, ByteOrder.LITTLE_ENDIAN);
if (magic != (short) GZIP_MAGIC) {
// from this stream.
return true;
}
// We've encountered the gzip magic number, so we assume there's another
// member in the stream.
parseGzipHeader(in, buffer, crc, buf);
return false;
}
use of java.io.PushbackInputStream in project XobotOS by xamarin.
the class JDKX509CertificateFactory method engineGenerateCRL.
/**
* Generates a certificate revocation list (CRL) object and initializes
* it with the data read from the input stream inStream.
*/
public CRL engineGenerateCRL(InputStream inStream) throws CRLException {
if (currentCrlStream == null) {
currentCrlStream = inStream;
sCrlData = null;
sCrlDataObjectCount = 0;
} else if (// reset if input stream has changed
currentCrlStream != inStream) {
currentCrlStream = inStream;
sCrlData = null;
sCrlDataObjectCount = 0;
}
try {
if (sCrlData != null) {
if (sCrlDataObjectCount != sCrlData.size()) {
return getCRL();
} else {
sCrlData = null;
sCrlDataObjectCount = 0;
return null;
}
}
int limit = ProviderUtil.getReadLimit(inStream);
PushbackInputStream pis = new PushbackInputStream(inStream);
int tag = pis.read();
if (tag == -1) {
return null;
}
pis.unread(tag);
if (// assume ascii PEM encoded.
tag != 0x30) {
return readPEMCRL(pis);
} else {
// lazy evaluate to help processing of large CRLs
return readDERCRL(new ASN1InputStream(pis, limit, true));
}
} catch (CRLException e) {
throw e;
} catch (Exception e) {
throw new CRLException(e.toString());
}
}
use of java.io.PushbackInputStream in project XobotOS by xamarin.
the class ZipInputStream method closeEntry.
/**
* Closes the current ZIP entry and positions to read the next entry.
*
* @throws IOException
* if an {@code IOException} occurs.
*/
public void closeEntry() throws IOException {
checkClosed();
if (currentEntry == null) {
return;
}
if (currentEntry instanceof java.util.jar.JarEntry) {
Attributes temp = ((JarEntry) currentEntry).getAttributes();
if (temp != null && temp.containsKey("hidden")) {
return;
}
}
/*
* The following code is careful to leave the ZipInputStream in a
* consistent state, even when close() results in an exception. It does
* so by:
* - pushing bytes back into the source stream
* - reading a data descriptor footer from the source stream
* - resetting fields that manage the entry being closed
*/
// Ensure all entry bytes are read
Exception failure = null;
try {
Streams.skipAll(this);
} catch (Exception e) {
failure = e;
}
int inB, out;
if (currentEntry.compressionMethod == ZipEntry.DEFLATED) {
inB = inf.getTotalIn();
out = inf.getTotalOut();
} else {
inB = inRead;
out = inRead;
}
int diff = entryIn - inB;
// Pushback any required bytes
if (diff != 0) {
((PushbackInputStream) in).unread(buf, len - diff, diff);
}
try {
readAndVerifyDataDescriptor(inB, out);
} catch (Exception e) {
if (failure == null) {
// otherwise we're already going to throw
failure = e;
}
}
inf.reset();
lastRead = inRead = entryIn = len = 0;
crc.reset();
currentEntry = null;
if (failure != null) {
if (failure instanceof IOException) {
throw (IOException) failure;
} else if (failure instanceof RuntimeException) {
throw (RuntimeException) failure;
}
AssertionError error = new AssertionError();
error.initCause(failure);
throw error;
}
}
use of java.io.PushbackInputStream in project zm-mailbox by Zimbra.
the class NativeFormatter method sendbackBinaryData.
public static void sendbackBinaryData(HttpServletRequest req, HttpServletResponse resp, InputStream in, String contentType, String disposition, String filename, long size, boolean ignoreContentDisposition) throws IOException {
resp.setContentType(contentType);
if (disposition == null) {
String disp = req.getParameter(UserServlet.QP_DISP);
disposition = (disp == null || disp.toLowerCase().startsWith("i")) ? Part.INLINE : Part.ATTACHMENT;
}
PushbackInputStream pis = new PushbackInputStream(in, READ_AHEAD_BUFFER_SIZE);
boolean isSafe = false;
HttpUtil.Browser browser = HttpUtil.guessBrowser(req);
if (browser != HttpUtil.Browser.IE) {
isSafe = true;
} else if (disposition.equals(Part.ATTACHMENT)) {
isSafe = true;
if (isScriptableContent(contentType)) {
// ask it to save the file
resp.addHeader("X-Download-Options", "noopen");
}
}
if (!isSafe) {
byte[] buf = new byte[READ_AHEAD_BUFFER_SIZE];
int bytesRead = pis.read(buf, 0, READ_AHEAD_BUFFER_SIZE);
boolean hasScript;
for (int i = 0; i < bytesRead; i++) {
if (buf[i] == SCRIPT_PATTERN[0][0] || buf[i] == SCRIPT_PATTERN[1][0]) {
hasScript = true;
for (int pos = 1; pos < 7 && (i + pos) < bytesRead; pos++) {
if (buf[i + pos] != SCRIPT_PATTERN[0][pos] && buf[i + pos] != SCRIPT_PATTERN[1][pos]) {
hasScript = false;
break;
}
}
if (hasScript) {
resp.addHeader("Cache-Control", "no-transform");
disposition = Part.ATTACHMENT;
break;
}
}
}
if (bytesRead > 0)
pis.unread(buf, 0, bytesRead);
}
if (!ignoreContentDisposition) {
String cd = HttpUtil.createContentDisposition(req, disposition, filename == null ? "unknown" : filename);
resp.addHeader("Content-Disposition", cd);
}
if (size > 0)
resp.setContentLength((int) size);
ByteUtil.copy(pis, true, resp.getOutputStream(), false);
}
Aggregations