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

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

Introduction

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

Prototype

public static OutputStream nullOutputStream() 

Source Link

Document

Returns a new OutputStream which discards all bytes.

Usage

From source file:org.culturegraph.mf.flux.FlowTest.java

@Test
public void testCommandRegistration() {
    // all commands must properly load to print the help
    FluxProgramm.printHelp(new PrintStream(new NullOutputStream()));

}

From source file:org.datacleaner.result.save.AnalysisResultSaveHandler.java

/**
 * Gets a map of unsafe result elements, ie. elements that cannot be saved
 * because serialization fails.//from   w  w w  . j ava 2s .  c  om
 *
 * @return
 */
public Map<ComponentJob, AnalyzerResult> getUnsafeResultElements() {
    if (_unsafeResultElements == null) {
        _unsafeResultElements = new LinkedHashMap<>();
        final Map<ComponentJob, AnalyzerResult> resultMap = _analysisResult.getResultMap();
        for (final Entry<ComponentJob, AnalyzerResult> entry : resultMap.entrySet()) {
            final AnalyzerResult analyzerResult = entry.getValue();
            try {
                SerializationUtils.serialize(analyzerResult, new NullOutputStream());
            } catch (final SerializationException e) {
                _unsafeResultElements.put(entry.getKey(), analyzerResult);
            }
        }
    }
    return _unsafeResultElements;
}

From source file:org.dataconservancy.access.connector.AbstractConnectorIT.java

/**
 * Asserts that the supplied input stream has the expected fixity.
 *
 * @param expectedFixity the expected fixity value of the stream
 * @param in             the input stream
 * @throws java.security.NoSuchAlgorithmException
 * @throws IOException//from  ww  w.ja  v  a  2s.co m
 */
void assertFixtyDigestEqual(final DcsFixity expectedFixity, final InputStream in)
        throws NoSuchAlgorithmException, IOException {
    Assert.assertNotNull("Fixity must not be null", expectedFixity);
    Assert.assertNotNull("Inputstream must not be null", in);

    final StringBuilder actualDigest = new StringBuilder();
    final NullOutputStream nullOs = new NullOutputStream();
    final DigestNotificationOutputStream digestOut = new DigestNotificationOutputStream(nullOs,
            MessageDigest.getInstance(expectedFixity.getAlgorithm()), new DigestListener() {
                @Override
                public void notify(byte[] digestValue) throws IOException {
                    for (byte b : digestValue) {
                        actualDigest.append(String.format("%02x", b));
                    }

                }
            });

    IOUtils.copy(in, digestOut);
    digestOut.close();
    assertEquals(expectedFixity.getValue(), actualDigest.toString());
}

From source file:org.dataconservancy.access.connector.AbstractHttpConnectorTest.java

/**
 * Asserts that the supplied input stream has the expected fixity.
 *
 * @param expectedFixity the expected fixity value of the stream
 * @param in             the input stream
 * @throws java.security.NoSuchAlgorithmException
 * @throws IOException//w w w. j a  v a 2  s . co m
 */
void assertFixtyDigestEqual(final DcsFixity expectedFixity, final InputStream in)
        throws NoSuchAlgorithmException, IOException {
    assertNotNull("Fixity must not be null", expectedFixity);
    assertNotNull("Inputstream must not be null", in);

    final StringBuilder actualDigest = new StringBuilder();
    final NullOutputStream nullOs = new NullOutputStream();
    final DigestNotificationOutputStream digestOut = new DigestNotificationOutputStream(nullOs,
            MessageDigest.getInstance(expectedFixity.getAlgorithm()), new DigestListener() {
                @Override
                public void notify(byte[] digestValue) throws IOException {
                    for (byte b : digestValue) {
                        actualDigest.append(String.format("%02x", b));
                    }

                }
            });

    IOUtils.copy(in, digestOut);
    digestOut.close();
    assertEquals(expectedFixity.getValue(), actualDigest.toString());
}

From source file:org.dataconservancy.access.connector.DcsConnectorRequestThread.java

@Override
public void run() {
    super.run();//from  ww  w .  ja v a 2s . c  o  m

    if (connector == null) {
        throw new IllegalStateException("HttpClient must not be null.");
    }

    try {
        InputStream in = connector.getStream(url);
        if (consumeResponse) {
            IOUtils.copy(in, new NullOutputStream());
        }
    } catch (ClientProtocolException e) {
        try {
            cpeHandler.handleException(e);
        } catch (ClientProtocolException e1) {
            // don't care
        }
    } catch (IOException e) {
        try {
            ioeHandler.handleException(e);
        } catch (IOException e1) {
            // don't care
        }
    } catch (DcsConnectorFault e) {
        try {
            dcsFaultHandler.handleException(e);
        } catch (Throwable throwable) {
            // don't care
        }
    }
}

From source file:org.dataconservancy.access.connector.HttpClientRequestThread.java

@Override
public void run() {
    super.run();//w w w  . ja va  2s.  c  om

    if (client == null) {
        throw new IllegalStateException("HttpClient must not be null.");
    }

    try {
        InputStream in = client.execute(new HttpGet(url)).getEntity().getContent();
        if (consumeResponse) {
            IOUtils.copy(in, new NullOutputStream());
        }
    } catch (ClientProtocolException e) {
        try {
            cpeHandler.handleException(e);
        } catch (ClientProtocolException e1) {
            // don't care
        }
    } catch (IOException e) {
        try {
            ioeHandler.handleException(e);
        } catch (IOException e1) {
            // don't care
        }
    }
}

From source file:org.dataconservancy.access.connector.HttpDcsConnectorTest.java

/**
 * Here we properly perform the requests.  The first request is issued, and its response is exhausted by IOUtils
 * (the underlying connection is freed by reading the stream), and then perform the second request.
 *
 * @throws IOException// www .jav  a  2s.co m
 * @throws DcsClientFault
 * @throws SAXException
 */
@Test
public void testSingleConnectionReuseScenarioOk() throws IOException, DcsClientFault, SAXException {
    assertTrue(new File(entitiesDir, "test1.xml").exists());
    assertTrue(new File(entitiesDir, "test2.xml").exists());
    final InputStream stream = underTest.getStream(config.getAccessHttpUrl() + "/entity/test1");
    IOUtils.copy(stream, new NullOutputStream());
    underTest.getStream(config.getAccessHttpUrl() + "/entity/test2");
}

From source file:org.dataconservancy.access.connector.HttpDcsConnectorTest.java

@Test
public void testSearchGetStreamOk() throws DcsClientFault, IOException {
    final NullOutputStream nullOutputStream = new NullOutputStream();
    final Iterator<DcsEntity> result = underTest.search("this can be any query");
    int count = 0;
    while (result.hasNext()) {
        try {//ww w .j av a 2  s. co m
            final InputStream stream = underTest.getStream(result.next().getId()
                    .replace("http://localhost:8080", config.getAccessHttpUrl().toString()));
            assertNotNull(stream);
            // exhaust the stream to free up connection
            IOUtils.copy(stream, nullOutputStream);
            count++;
        } catch (DcsClientFault dcsClientFault) {
            //                log.debug(dcsClientFault.getMessage());
        } catch (IOException e) {
            //                log.debug(e.getMessage());
        }
    }

    assertEquals(126, count);
}

From source file:org.dataconservancy.dcs.lineage.http.support.RequestUtil.java

/**
 * Calculates a MD5 digest over the list of entity ids, suitable for use as an
 * ETag.  If the list is empty, {@code null} is returned.
 *
 * @param entityIds the entities//from w  ww  .  j  a v  a  2  s .c o m
 * @return the MD5 digest, or {@code null} if {@code entityIds} is empty.
 */
public static String calculateDigest(List<String> entityIds) {
    if (entityIds.isEmpty()) {
        return null;
    }

    NullOutputStream nullOut = new NullOutputStream();
    DigestOutputStream digestOut = null;

    try {
        digestOut = new DigestOutputStream(nullOut, MessageDigest.getInstance("MD5"));
        for (String id : entityIds) {
            IOUtils.write(id, digestOut);
        }
        digestOut.close();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    return digestToHexString(digestOut.getMessageDigest().digest());
}

From source file:org.dataconservancy.dcs.util.DigestNotificationStreamTest.java

@Test
public void listenerNotifiedOnceTest() throws IOException, NoSuchAlgorithmException {
    final AtomicInteger count = new AtomicInteger(0);
    final InputStream s = new DigestNotificationStream(new ByteArrayInputStream(CONTENT.getBytes()),
            MessageDigest.getInstance("MD5"), new DigestListener() {
                @Override/*from  www. ja va 2s.  c om*/
                public void notify(byte[] digestValue) throws IOException {
                    count.getAndIncrement();
                }
            });

    assertEquals(0, count.intValue());
    IOUtils.copy(s, new NullOutputStream());
    assertEquals(1, count.intValue());
    s.close();
    assertEquals(1, count.intValue());
}