Example usage for org.apache.commons.io.output NullOutputStream NULL_OUTPUT_STREAM

List of usage examples for org.apache.commons.io.output NullOutputStream NULL_OUTPUT_STREAM

Introduction

In this page you can find the example usage for org.apache.commons.io.output NullOutputStream NULL_OUTPUT_STREAM.

Prototype

NullOutputStream NULL_OUTPUT_STREAM

To view the source code for org.apache.commons.io.output NullOutputStream NULL_OUTPUT_STREAM.

Click Source Link

Document

A singleton.

Usage

From source file:org.apereo.portal.portlet.container.cache.CachingPortletOutputHandlerTest.java

@Test
public void testTooMuchStreamContentThenReset() throws IOException {
    final CachingPortletOutputHandler cachingOutputHandler = new CachingPortletOutputHandler(
            portletOutputHandler, 100);//ww  w. j av a 2 s  . com

    when(portletOutputHandler.getOutputStream()).thenReturn(NullOutputStream.NULL_OUTPUT_STREAM);

    final String output = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";

    final OutputStream outputStream = cachingOutputHandler.getOutputStream();
    outputStream.write(output.getBytes());
    outputStream.write(output.getBytes());

    CachedPortletData<Long> cachedPortletData = cachingOutputHandler.getCachedPortletData(1l,
            new CacheControlImpl());
    assertNull(cachedPortletData);

    cachingOutputHandler.reset();

    outputStream.write(output.getBytes());

    cachedPortletData = cachingOutputHandler.getCachedPortletData(1l, new CacheControlImpl());
    assertNotNull(cachedPortletData);
    assertArrayEquals(output.getBytes(), cachedPortletData.getCachedStreamOutput());
}

From source file:org.apereo.portal.portlet.container.cache.LimitingTeeOutputStream.java

@Override
protected void beforeWrite(int n) throws IOException {
    this.byteCount += n;

    if (this.maximumBytes > 0 && !this.limitReached && this.byteCount > this.maximumBytes) {
        //Hit limit, replace tee'd OutputStream with a null OutputStream
        this.limitReached = true;
        this.setBranch(NullOutputStream.NULL_OUTPUT_STREAM);

        if (this.limitReachedCallback != null) {
            this.limitReachedCallback.apply(this);
        }//from  ww  w. j av  a2s  .c  o m
    }
}

From source file:org.apereo.portal.portlet.container.cache.LimitingTeeOutputStreamTest.java

@Test
public void testControl() throws IOException {
    final byte[] content = "<p>Simple content</p>".getBytes();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    LimitingTeeOutputStream stream = new LimitingTeeOutputStream(content.length,
            NullOutputStream.NULL_OUTPUT_STREAM, byteStream);
    stream.write(content);/*  ww  w .  j  a v a  2  s  .  co  m*/

    assertFalse(stream.isLimitReached());
    assertArrayEquals(content, byteStream.toByteArray());
}

From source file:org.apereo.portal.portlet.container.cache.LimitingTeeOutputStreamTest.java

@Test
public void testContentExceedsThreshold() throws IOException {
    final byte[] content = "<p>Simple content</p>".getBytes();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    LimitingTeeOutputStream stream = new LimitingTeeOutputStream(content.length - 1,
            NullOutputStream.NULL_OUTPUT_STREAM, byteStream);
    stream.write(content);/*from w  w w  .j av  a  2  s  . co m*/

    assertTrue(stream.isLimitReached());
    assertArrayEquals(new byte[0], byteStream.toByteArray());
    // try to write more and see no results
    stream.write("a".getBytes());
    assertArrayEquals(new byte[0], byteStream.toByteArray());
}

From source file:org.apereo.portal.portlet.container.cache.LimitingTeeOutputStreamTest.java

@Test
public void testContentExceedsClearBuffer() throws IOException {
    final byte[] content = "<p>Simple content</p>".getBytes();
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    LimitingTeeOutputStream stream = new LimitingTeeOutputStream(content.length - 1,
            NullOutputStream.NULL_OUTPUT_STREAM, byteStream, new Function<LimitingTeeOutputStream, Object>() {
                @Override/* w w  w . ja  va2  s . c o m*/
                public Object apply(LimitingTeeOutputStream input) {
                    byteStream.reset();
                    return null;
                }
            });
    // write the first few chars
    stream.write(content, 0, 5);
    // verify content successfully buffered
    assertFalse(stream.isLimitReached());
    final byte[] subContent = Arrays.copyOf(content, 5);
    assertArrayEquals(subContent, byteStream.toByteArray());

    // now write the remainder 
    stream.write(content, 5, content.length);

    assertTrue(stream.isLimitReached());
    assertArrayEquals(new byte[0], byteStream.toByteArray());
    // try to write more and see no results
    stream.write("a".getBytes());
    assertArrayEquals(new byte[0], byteStream.toByteArray());
}

From source file:org.apereo.portal.portlet.container.cache.LimitingTeeOutputStreamTest.java

@Test
public void testContentExceedsResetBuffer() throws IOException {
    final byte[] content = "<p>Simple content</p>".getBytes();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    LimitingTeeOutputStream stream = new LimitingTeeOutputStream(content.length - 1,
            NullOutputStream.NULL_OUTPUT_STREAM, byteStream);
    stream.write(content);// www.  j  av a  2  s  .c om

    assertTrue(stream.isLimitReached());
    assertArrayEquals(new byte[0], byteStream.toByteArray());

    stream.resetByteCount();

    // try to write more and see results
    stream.write("a".getBytes());
    assertArrayEquals("a".getBytes(), byteStream.toByteArray());
}

From source file:org.apereo.portal.utils.web.PortletHttpServletResponseWrapper.java

@Override
public ServletOutputStream getOutputStream() throws IOException {
    if (this.servletOutputStream == null) {
        final OutputStream out;
        if (logger.isDebugEnabled()) {
            out = new ByteArrayOutputStream() {
                @Override//  w  w  w .  j  a v a  2s. c  o  m
                public void close() throws IOException {
                    super.close();
                    final byte[] data = this.toByteArray();
                    if (data.length > 0) {
                        logger.warn("Ignored {} bytes written to ServletOutputStream by {}\n\n{}",
                                new Object[] { data.length, portletWindow, new String(data) });
                    }
                }
            };
        } else {
            out = new CountingOutputStream(NullOutputStream.NULL_OUTPUT_STREAM) {
                @Override
                public void close() throws IOException {
                    super.close();
                    final long byteCount = this.getByteCount();
                    if (byteCount > 0) {
                        logger.warn(
                                "Ignored {} bytes written to ServletOutputStream by {}, turn on DEBUG logging to see the output",
                                byteCount, portletWindow);
                    }
                }
            };
        }

        this.servletOutputStream = new DelegatingServletOutputStream(out);
    }

    return this.servletOutputStream;
}

From source file:org.broadinstitute.gatk.utils.runtime.CapturedStreamOutput.java

/**
 * @param settings       Settings that define what to capture.
 * @param processStream  Stream to capture output.
 * @param standardStream Stream to write debug output.
 *///from  w  ww.j  a  v a2  s .c  o  m
public CapturedStreamOutput(OutputStreamSettings settings, InputStream processStream,
        PrintStream standardStream) {
    this.processStream = processStream;
    int bufferSize = settings.getBufferSize();
    this.bufferStream = (bufferSize < 0) ? new ByteArrayOutputStream() : new ByteArrayOutputStream(bufferSize);

    for (StreamLocation location : settings.getStreamLocations()) {
        OutputStream outputStream;
        switch (location) {
        case Buffer:
            if (bufferSize < 0) {
                outputStream = this.bufferStream;
            } else {
                outputStream = new HardThresholdingOutputStream(bufferSize) {
                    @Override
                    protected OutputStream getStream() throws IOException {
                        return bufferTruncated ? NullOutputStream.NULL_OUTPUT_STREAM : bufferStream;
                    }

                    @Override
                    protected void thresholdReached() throws IOException {
                        bufferTruncated = true;
                    }
                };
            }
            break;
        case File:
            try {
                outputStream = new FileOutputStream(settings.getOutputFile(), settings.isAppendFile());
            } catch (IOException e) {
                throw new UserException.BadInput(e.getMessage());
            }
            break;
        case Standard:
            outputStream = standardStream;
            break;
        default:
            throw new ReviewedGATKException("Unexpected stream location: " + location);
        }
        this.outputStreams.put(location, outputStream);
    }
}

From source file:org.broadinstitute.sting.utils.runtime.CapturedStreamOutput.java

/**
 * @param settings       Settings that define what to capture.
 * @param processStream  Stream to capture output.
 * @param standardStream Stream to write debug output.
 *///from w  ww .ja v a  2s .  c om
public CapturedStreamOutput(OutputStreamSettings settings, InputStream processStream,
        PrintStream standardStream) {
    this.processStream = processStream;
    int bufferSize = settings.getBufferSize();
    this.bufferStream = (bufferSize < 0) ? new ByteArrayOutputStream() : new ByteArrayOutputStream(bufferSize);

    for (StreamLocation location : settings.getStreamLocations()) {
        OutputStream outputStream;
        switch (location) {
        case Buffer:
            if (bufferSize < 0) {
                outputStream = this.bufferStream;
            } else {
                outputStream = new HardThresholdingOutputStream(bufferSize) {
                    @Override
                    protected OutputStream getStream() throws IOException {
                        return bufferTruncated ? NullOutputStream.NULL_OUTPUT_STREAM : bufferStream;
                    }

                    @Override
                    protected void thresholdReached() throws IOException {
                        bufferTruncated = true;
                    }
                };
            }
            break;
        case File:
            try {
                outputStream = new FileOutputStream(settings.getOutputFile(), settings.isAppendFile());
            } catch (IOException e) {
                throw new UserException.BadInput(e.getMessage());
            }
            break;
        case Standard:
            outputStream = standardStream;
            break;
        default:
            throw new ReviewedStingException("Unexpected stream location: " + location);
        }
        this.outputStreams.put(location, outputStream);
    }
}

From source file:org.eclipse.kura.linux.net.modem.SupportedUsbModems.java

/**
 * Execute command an return splitted lines
 *
 * @param command/*from w w w . ja  va 2 s  .c o  m*/
 *            the command to execute
 * @return the lines output by the command
 * @throws IOException
 *             if executing the commands fails
 */
private static List<String> execute(final String command) throws ExecuteException, IOException {
    final DefaultExecutor executor = new DefaultExecutor();

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(out, NullOutputStream.NULL_OUTPUT_STREAM));

    int rc = executor.execute(CommandLine.parse(command));

    s_logger.debug("Called {} - rc = {}", command, rc);

    return IOUtils.readLines(new ByteArrayInputStream(out.toByteArray()));
}