Search in sources :

Example 61 with PushbackInputStream

use of java.io.PushbackInputStream in project tomee 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 62 with PushbackInputStream

use of java.io.PushbackInputStream in project tomee 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 63 with PushbackInputStream

use of java.io.PushbackInputStream in project dubbo by alibaba.

the class StreamUtilsTest method testMarkSupportedInputStream.

@Test
public void testMarkSupportedInputStream() throws Exception {
    InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt");
    assertEquals(10, is.available());
    is = new PushbackInputStream(is);
    assertEquals(10, is.available());
    assertFalse(is.markSupported());
    is = StreamUtils.markSupportedInputStream(is);
    assertEquals(10, is.available());
    is.mark(0);
    assertEquals((int) '0', is.read());
    assertEquals((int) '1', is.read());
    is.reset();
    assertEquals((int) '0', is.read());
    assertEquals((int) '1', is.read());
    assertEquals((int) '2', is.read());
    is.mark(0);
    assertEquals((int) '3', is.read());
    assertEquals((int) '4', is.read());
    assertEquals((int) '5', is.read());
    is.reset();
    assertEquals((int) '3', is.read());
    assertEquals((int) '4', is.read());
    is.mark(0);
    assertEquals((int) '5', is.read());
    assertEquals((int) '6', is.read());
    is.reset();
    assertEquals((int) '5', is.read());
    assertEquals((int) '6', is.read());
    assertEquals((int) '7', is.read());
    assertEquals((int) '8', is.read());
    assertEquals((int) '9', is.read());
    assertEquals(-1, is.read());
    assertEquals(-1, is.read());
    is.mark(0);
    assertEquals(-1, is.read());
    assertEquals(-1, is.read());
    is.reset();
    assertEquals(-1, is.read());
    assertEquals(-1, is.read());
    is.close();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 64 with PushbackInputStream

use of java.io.PushbackInputStream in project dubbo by alibaba.

the class StreamUtilsTest method testMarkInputSupport.

@Test
public void testMarkInputSupport() {
    Assertions.assertThrows(IOException.class, () -> {
        InputStream is = StreamUtilsTest.class.getResourceAsStream("/StreamUtilsTest.txt");
        try {
            is = StreamUtils.markSupportedInputStream(new PushbackInputStream(is), 1);
            is.mark(1);
            int read = is.read();
            assertThat(read, is((int) '0'));
            is.skip(1);
            is.read();
        } finally {
            if (is != null) {
                is.close();
            }
        }
    });
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 65 with PushbackInputStream

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

the class PrependableSocket method prependToInputStream.

/**
 * Prepend some bytes that have already been read back to the socket's input stream. Note that this method can be
 * called at most once with a non-0 length per socket instance.
 * @param bytes the bytes to prepend.
 * @param offset offset in the byte array to start at.
 * @param length number of bytes to prepend.
 * @throws IOException if this method was already called on the socket instance, or if super.getInputStream() throws.
 */
public void prependToInputStream(byte[] bytes, int offset, int length) throws IOException {
    if (length == 0) {
        // nothing to prepend
        return;
    }
    if (pushbackInputStream != null) {
        throw new IOException("prependToInputStream() called more than once");
    }
    PushbackInputStream pushbackInputStream = new PushbackInputStream(getInputStream(), length);
    pushbackInputStream.unread(bytes, offset, length);
    this.pushbackInputStream = pushbackInputStream;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException)

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