Search in sources :

Example 66 with PushbackInputStream

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);
}
Also used : PushbackInputStream(java.io.PushbackInputStream) HttpUtil(com.zimbra.common.util.HttpUtil)

Example 67 with PushbackInputStream

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;
}
Also used : HttpEntity(org.apache.http.HttpEntity) PushbackInputStream(java.io.PushbackInputStream)

Example 68 with PushbackInputStream

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();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 69 with PushbackInputStream

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;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException)

Example 70 with PushbackInputStream

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));
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)130 IOException (java.io.IOException)61 InputStream (java.io.InputStream)34 ByteArrayInputStream (java.io.ByteArrayInputStream)21 FileInputStream (java.io.FileInputStream)10 GZIPInputStream (java.util.zip.GZIPInputStream)10 InputStreamReader (java.io.InputStreamReader)8 File (java.io.File)5 CertificateException (java.security.cert.CertificateException)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedReader (java.io.BufferedReader)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Charset (java.nio.charset.Charset)3