Example usage for org.apache.commons.io.input BoundedInputStream BoundedInputStream

List of usage examples for org.apache.commons.io.input BoundedInputStream BoundedInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input BoundedInputStream BoundedInputStream.

Prototype

public BoundedInputStream(InputStream in, long size) 

Source Link

Document

Creates a new BoundedInputStream that wraps the given input stream and limits it to a certain size.

Usage

From source file:org.apache.streams.facebook.test.data.FacebookPageSerDeIT.java

@Test
public void Tests() {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookPageSerDeIT.class.getResourceAsStream("/testpage.json");
    is = new BoundedInputStream(is, 10000);
    String json;//from  w ww  .  j a v  a  2  s  .co  m

    try {
        json = String.join(" ", IOUtils.readLines(is, Charset.defaultCharset()));
        LOGGER.debug(json);

        Page ser = mapper.readValue(json, Page.class);

        String de = mapper.writeValueAsString(ser);

        LOGGER.debug(de);

        Page serde = mapper.readValue(de, Page.class);

        Assert.assertEquals(ser, serde);

        LOGGER.debug(mapper.writeValueAsString(serde));

    } catch (Exception e) {
        LOGGER.error("Exception: ", e);
        Assert.fail();
    }
}

From source file:org.apache.streams.facebook.test.data.FacebookPostSerDeIT.java

@Test
public void Tests() {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookPostSerDeIT.class.getResourceAsStream("/testpost.json");
    is = new BoundedInputStream(is, 10000);
    String json;//from w w w .jav a 2  s  . com

    try {
        json = String.join(" ", IOUtils.readLines(is, Charset.defaultCharset()));
        LOGGER.debug(json);

        Post ser = mapper.readValue(json, Post.class);

        String de = mapper.writeValueAsString(ser);

        LOGGER.debug(de);

        Post serde = mapper.readValue(de, Post.class);

        assertEquals(ser, serde);

        LOGGER.debug(mapper.writeValueAsString(serde));

        Activity activity = new Activity();
        FacebookActivityUtil.updateActivity(ser, activity);

        assertNotNull(activity);
        assertNotNull(activity.getActor().getId());
        assertNotNull(activity.getActor().getDisplayName());
        assertNotNull(activity.getId());
        assert (activity.getVerb().equals("post"));
        assertNotNull(activity.getObject());
        assertNotNull(activity.getUpdated());
        assertNotNull(activity.getPublished());
        assertEquals(activity.getProvider().getId(), "id:providers:facebook");
        assertEquals(activity.getProvider().getDisplayName(), "Facebook");
        assertEquals(activity.getLinks().size(), 1);
        assertNotNull(activity.getAdditionalProperties().get("facebook"));

    } catch (Exception e) {
        LOGGER.error("Exception: ", e);
        Assert.fail();
    }
}

From source file:org.apache.streams.facebook.test.FacebookActivityActorSerDeTest.java

@Test
public void Tests() throws Exception {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookActivityActorSerDeTest.class.getResourceAsStream("/testpage.json");
    Joiner joiner = Joiner.on(" ").skipNulls();
    is = new BoundedInputStream(is, 10000);
    String json;/*from   ww  w.  j  a v  a2s  .c  om*/

    json = joiner.join(IOUtils.readLines(is));
    LOGGER.debug(json);

    Page page = mapper.readValue(json, Page.class);

    Activity activity = serializer.deserialize(page);

    LOGGER.debug(mapper.writeValueAsString(activity));
}

From source file:org.apache.streams.facebook.test.FacebookActivitySerDeTest.java

@Test
public void Tests() {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookActivitySerDeTest.class.getResourceAsStream("/testpost.json");
    Joiner joiner = Joiner.on(" ").skipNulls();
    is = new BoundedInputStream(is, 10000);
    String json;/*from   w ww .  ja va 2 s  . c  om*/

    try {
        json = joiner.join(IOUtils.readLines(is));
        LOGGER.debug(json);

        Post post = mapper.readValue(json, Post.class);

        Activity activity = serializer.deserialize(post);

        LOGGER.debug(mapper.writeValueAsString(activity));

    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:org.apache.streams.facebook.test.FacebookPageSerDeTest.java

@Test
public void Tests() {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookPostSerDeTest.class.getResourceAsStream("/testpage.json");
    Joiner joiner = Joiner.on(" ").skipNulls();
    is = new BoundedInputStream(is, 10000);
    String json;/*from   w w  w.  jav  a  2 s .com*/

    try {
        json = joiner.join(IOUtils.readLines(is));
        LOGGER.debug(json);

        Page ser = mapper.readValue(json, Page.class);

        String de = mapper.writeValueAsString(ser);

        LOGGER.debug(de);

        Page serde = mapper.readValue(de, Page.class);

        Assert.assertEquals(ser, serde);

        LOGGER.debug(mapper.writeValueAsString(serde));

    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:org.apache.streams.facebook.test.FacebookPostSerDeTest.java

@Test
public void Tests() {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FacebookPostSerDeTest.class.getResourceAsStream("/testpost.json");
    Joiner joiner = Joiner.on(" ").skipNulls();
    is = new BoundedInputStream(is, 10000);
    String json;//from www. ja v  a 2 s . co m

    try {
        json = joiner.join(IOUtils.readLines(is));
        LOGGER.debug(json);

        Post ser = mapper.readValue(json, Post.class);

        String de = mapper.writeValueAsString(ser);

        LOGGER.debug(de);

        Post serde = mapper.readValue(de, Post.class);

        Assert.assertEquals(ser, serde);

        LOGGER.debug(mapper.writeValueAsString(serde));

        Activity activity = new Activity();
        FacebookActivityUtil.updateActivity(ser, activity);

        assertNotNull(activity);
        assertNotNull(activity.getActor().getId());
        assertNotNull(activity.getActor().getDisplayName());
        assertNotNull(activity.getId());
        assert (activity.getVerb().equals("post"));
        assertNotNull(activity.getObject());
        assertNotNull(activity.getUpdated());
        assertNotNull(activity.getPublished());
        assertEquals(activity.getProvider().getId(), "id:providers:facebook");
        assertEquals(activity.getProvider().getDisplayName(), "Facebook");
        assertEquals(activity.getLinks().size(), 1);
        assertNotNull(activity.getAdditionalProperties().get("extensions"));

    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:org.apache.tez.runtime.library.common.shuffle.LocalDiskFetchedInput.java

@Override
public InputStream getInputStream() throws IOException {
    FSDataInputStream inputStream = localFS.open(inputFile);
    inputStream.seek(startOffset);/*from   w  w w. j a  v  a2s .  com*/
    return new BoundedInputStream(inputStream, getSize());
}

From source file:org.apache.tez.runtime.library.shuffle.common.LocalDiskFetchedInput.java

@Override
public InputStream getInputStream() throws IOException {
    FSDataInputStream inputStream = localFS.open(inputFile);
    inputStream.seek(startOffset);/* w w w  .  ja va  2  s.  co  m*/
    return new BoundedInputStream(inputStream, compressedSize);
}

From source file:org.apache.tika.parser.html.charsetdetector.StandardHtmlEncodingDetector.java

@Override
public Charset detect(InputStream input, Metadata metadata) throws IOException {
    int limit = getMarkLimit();
    input.mark(limit);//from   w  w  w  .jav  a2 s. c om
    // Never read more than the first META_TAG_BUFFER_SIZE bytes
    InputStream limitedStream = new BoundedInputStream(input, limit);
    PreScanner preScanner = new PreScanner(limitedStream);

    // The order of priority for detection is:
    // 1. Byte Order Mark
    Charset detectedCharset = preScanner.detectBOM();
    // 2. Transport-level information (Content-Type HTTP header)
    if (detectedCharset == null)
        detectedCharset = charsetFromContentType(metadata);
    // 3. HTML <meta> tag
    if (detectedCharset == null)
        detectedCharset = preScanner.scan();

    input.reset();
    return detectedCharset;
}

From source file:org.apache.wicket.request.resource.PartWriterCallback.java

/**
 * Writes the data//from w w  w .j a  va2  s  . com
 *
 * @param attributes
 *            the attributes to get the output stream of the response
 * @throws IOException
 *             if something went wrong while writing the data to the output stream
 */
@Override
public void writeData(Attributes attributes) throws IOException {
    try {
        OutputStream outputStream = attributes.getResponse().getOutputStream();
        byte[] buffer = new byte[getBufferSize()];

        if (startbyte != null || endbyte != null) {
            // skipping the first bytes which are
            // requested to be skipped by the client
            if (startbyte != null) {
                inputStream.skip(startbyte);
            } else {
                // If no start byte has been given set it to 0
                // which means no bytes has been skipped
                startbyte = 0L;
            }

            // If there are no end bytes given read the whole stream till the end
            if (endbyte == null || Long.valueOf(-1).equals(endbyte)) {
                endbyte = contentLength;
            }

            BoundedInputStream boundedInputStream = null;
            try {
                // Stream is going to be read from the starting point next to the skipped bytes
                // till the end byte computed by the range between startbyte / endbyte
                boundedInputStream = new BoundedInputStream(inputStream, (endbyte - startbyte) + 1);

                // The original input stream is going to be closed by the end of the request
                // so set propagate close to false
                boundedInputStream.setPropagateClose(false);

                // The read bytes in the current buffer
                int readBytes;

                while ((readBytes = boundedInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
            } finally {
                IOUtils.closeQuietly(boundedInputStream);
            }
        } else {
            // No range has been given so copy the content
            // from input stream to the output stream
            Streams.copy(inputStream, outputStream, getBufferSize());
        }
    } catch (ResponseIOException e) {
        // the client has closed the connection and
        // doesn't read the stream further on
        // (in tomcats
        // org.apache.catalina.connector.ClientAbortException)
        // we ignore this case
    }
    if (close) {
        IOUtils.close(inputStream);
    }
}