Search in sources :

Example 71 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class AttachmentDeserializer method initializeRootMessage.

protected void initializeRootMessage() throws IOException {
    String contentType = (String) message.get(Message.CONTENT_TYPE);
    if (contentType == null) {
        throw new IllegalStateException("Content-Type can not be empty!");
    }
    if (message.getContent(InputStream.class) == null) {
        throw new IllegalStateException("An InputStream must be provided!");
    }
    if (AttachmentUtil.isTypeSupported(contentType.toLowerCase(), supportedTypes)) {
        String boundaryString = findBoundaryFromContentType(contentType);
        if (null == boundaryString) {
            boundaryString = findBoundaryFromInputStream();
        }
        // If a boundary still wasn't found, throw an exception
        if (null == boundaryString) {
            throw new IOException("Couldn't determine the boundary from the message!");
        }
        boundary = boundaryString.getBytes(StandardCharsets.UTF_8);
        stream = new PushbackInputStream(message.getContent(InputStream.class), PUSHBACK_AMOUNT);
        if (!readTillFirstBoundary(stream, boundary)) {
            throw new IOException("Couldn't find MIME boundary: " + boundaryString);
        }
        Map<String, List<String>> ih = loadPartHeaders(stream);
        message.put(ATTACHMENT_PART_HEADERS, ih);
        String val = AttachmentUtil.getHeader(ih, "Content-Type", "; ");
        if (!StringUtils.isEmpty(val)) {
            String cs = HttpHeaderHelper.findCharset(val);
            if (!StringUtils.isEmpty(cs)) {
                message.put(Message.ENCODING, HttpHeaderHelper.mapCharset(cs));
            }
        }
        val = AttachmentUtil.getHeader(ih, "Content-Transfer-Encoding");
        MimeBodyPartInputStream mmps = new MimeBodyPartInputStream(stream, boundary, PUSHBACK_AMOUNT);
        InputStream ins = AttachmentUtil.decode(mmps, val);
        if (ins != mmps) {
            ih.remove("Content-Transfer-Encoding");
        }
        body = new DelegatingInputStream(ins, this);
        createCount++;
        message.setContent(InputStream.class, body);
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 72 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class AttachmentDeserializer method findBoundaryFromInputStream.

private String findBoundaryFromInputStream() throws IOException {
    InputStream is = message.getContent(InputStream.class);
    // boundary should definitely be in the first 2K;
    PushbackInputStream in = new PushbackInputStream(is, 4096);
    byte[] buf = new byte[2048];
    int i = in.read(buf);
    int len = i;
    while (i > 0 && len < buf.length) {
        i = in.read(buf, len, buf.length - len);
        if (i > 0) {
            len += i;
        }
    }
    String msg = IOUtils.newStringFromBytes(buf, 0, len);
    in.unread(buf, 0, len);
    // Reset the input stream since we'll need it again later
    message.setContent(InputStream.class, in);
    // Use regex to get the boundary and return null if it's not found
    Matcher m = INPUT_STREAM_BOUNDARY_PATTERN.matcher(msg);
    return m.find() ? "--" + m.group(1) : null;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) Matcher(java.util.regex.Matcher) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream)

Example 73 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class JAXRSMultipartTest method testBookAsMassiveAttachment.

@Test
public void testBookAsMassiveAttachment() throws Exception {
    // CXF-5842
    int orig = countTempFiles();
    String address = "http://localhost:" + PORT + "/bookstore/books/attachments";
    InputStream is = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/attachmentData");
    // create a stream that sticks a bunch of data for the attachement to cause the
    // server to buffer the attachment to disk.
    PushbackInputStream buf = new PushbackInputStream(is, 1024 * 20) {

        int bcount = -1;

        @Override
        public int read(byte[] b, int offset, int len) throws IOException {
            if (bcount >= 0 && bcount < 1024 * 50) {
                for (int x = 0; x < len; x++) {
                    b[offset + x] = (byte) x;
                }
                bcount += len;
                return len;
            }
            int i = super.read(b, offset, len);
            for (int x = 0; x < i - 5; x++) {
                if (b[x + offset] == '*' && b[x + offset + 1] == '*' && b[x + offset + 2] == 'D' && b[x + offset + 3] == '*' && b[x + offset + 4] == '*') {
                    super.unread(b, x + offset + 5, i - x - 5);
                    i = x;
                    bcount = 0;
                }
            }
            return i;
        }
    };
    doAddBook("multipart/related", address, buf, 413);
    assertEquals(orig, countTempFiles());
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 74 with PushbackInputStream

use of java.io.PushbackInputStream in project cxf by apache.

the class ChunkedUtil method getNonEmptyContent.

/**
 * @param connection the given HttpURLConnection
 * @return an input stream containing the response content if non-empty
 */
private static InputStream getNonEmptyContent(HttpURLConnection connection) {
    InputStream in = null;
    try {
        PushbackInputStream pin = new PushbackInputStream(connection.getInputStream());
        int c = pin.read();
        if (c != -1) {
            pin.unread((byte) c);
            in = pin;
        }
    } catch (IOException ioe) {
    // ignore
    }
    return in;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 75 with PushbackInputStream

use of java.io.PushbackInputStream in project omegat by omegat-org.

the class StaticUtils method extractFromZip.

/**
 * Extracts files from an InputStream representing a zip archive to the specified destination path.
 *
 * @param in
 *            InputStream representing a zip archive
 * @param destination
 *            Path where archive entries will be saved
 * @param filenameFilter
 *            Filter for entry names. Return false to skip extracting an entry
 * @return List of extracted entry names
 * @throws IOException
 */
public static List<String> extractFromZip(InputStream in, File destination, Predicate<String> filenameFilter) throws IOException {
    List<String> extracted = new ArrayList<>();
    try (PushbackInputStream pis = new PushbackInputStream(in, 2)) {
        byte[] sig = new byte[2];
        pis.read(sig);
        if (!(sig[0] == 0x50 && sig[1] == 0x4b)) {
            throw new IllegalArgumentException("Input stream was not a zip file");
        }
        pis.unread(sig);
        try (ZipInputStream zis = new ZipInputStream(pis)) {
            // parse the entries
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (filenameFilter.test(entry.getName())) {
                    // match found
                    File f = new File(destination, entry.getName());
                    FileUtils.copyToFile(zis, f);
                    extracted.add(entry.getName());
                }
            }
        }
    }
    return extracted;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) PushbackInputStream(java.io.PushbackInputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) File(java.io.File)

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