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);
}
use of java.io.PushbackInputStream in project wikidata-query-rdf by wikimedia.
the class WikibaseRepository method getInputStream.
/**
* Get input stream for entity, if it exists and not empty.
* We need this because our proxy can convert 204 responses
* to 200 responses with empty body.
*/
private InputStream getInputStream(HttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
PushbackInputStream in = new PushbackInputStream(entity.getContent());
int firstByte = in.read();
if (firstByte != -1) {
in.unread(firstByte);
return in;
}
}
return null;
}
use of java.io.PushbackInputStream in project cxf by apache.
the class AttachmentDeserializerTest method testSmallStream.
@Test
public void testSmallStream() throws Exception {
byte[] messageBytes = ("------=_Part_1\n\nJJJJ\n------=_Part_1\n\n" + "Content-Transfer-Encoding: binary\n\n=3D=3D=3D\n------=_Part_1\n").getBytes();
PushbackInputStream pushbackStream = new PushbackInputStream(new ByteArrayInputStream(messageBytes), 2048);
pushbackStream.read(new byte[4096], 0, 4015);
pushbackStream.unread(messageBytes);
pushbackStream.read(new byte[72]);
MimeBodyPartInputStream m = new MimeBodyPartInputStream(pushbackStream, "------=_Part_1".getBytes(), 2048);
assertEquals(10, m.read(new byte[1000]));
assertEquals(-1, m.read(new byte[1000]));
assertEquals(-1, m.read(new byte[1000]));
m.close();
}
use of java.io.PushbackInputStream in project cxf by apache.
the class IOUtils method isEmpty.
public static boolean isEmpty(InputStream is) throws IOException {
if (is == null) {
return true;
}
try {
// if available is 0 it does not mean it is empty
if (is.available() > 0) {
return false;
}
} catch (IOException ioe) {
// Do nothing
}
final byte[] bytes = new byte[1];
if (is.markSupported()) {
is.mark(1);
try {
return isEof(is.read(bytes));
} finally {
is.reset();
}
}
if (!(is instanceof PushbackInputStream)) {
return false;
}
// it may be an attachment stream
PushbackInputStream pbStream = (PushbackInputStream) is;
boolean isEmpty = isEof(pbStream.read(bytes));
if (!isEmpty) {
pbStream.unread(bytes);
}
return isEmpty;
}
use of java.io.PushbackInputStream in project cxf by apache.
the class IOUtilsTest method testNonEmptyWithPushBack.
@Test
public void testNonEmptyWithPushBack() throws Exception {
InputStream is = new PushbackInputStream(new ByteArrayInputStream("Hello".getBytes()));
assertFalse(IOUtils.isEmpty(is));
assertEquals("Hello", IOUtils.toString(is));
}
Aggregations