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

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

Introduction

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

Prototype

public NullInputStream(long size) 

Source Link

Document

Create an InputStream that emulates a specified size which supports marking and does not throw EOFException.

Usage

From source file:com.intel.cosbench.api.mock.MockStorage.java

@Override
public InputStream getObject(String container, String object, Config config) {
    super.getObject(container, object, config);
    if (profiling && logger.isDebugEnabled())
        stats.addEvent("GET", container + "/" + object);
    if (random.nextDouble() < errors)
        throw new StorageException("error injection");
    thread = Thread.currentThread();
    MockUtils.sleep(delay);/*w w w .j  a v  a 2 s.  c  om*/
    return new NullInputStream(size);
}

From source file:com.norconex.commons.lang.io.CachedInputStreamTest.java

@Test
public void testContentMatchPoolMaxFileCache() throws IOException {
    CachedStreamFactory factory = new CachedStreamFactory(200 * 1024, 150 * 1024);
    CachedInputStream cache1 = null;/*from   w w w.j  a  va 2s .c o  m*/
    CachedInputStream cache2 = null;
    try {
        // first time loads 6 bytes
        cache1 = factory.newInputStream(new NullInputStream(140 * 1024));
        readCacheToString(cache1);

        // first time loads 6 bytes, totaling 12, forcing file cache
        cache2 = factory.newInputStream(new NullInputStream(140 * 1024));
        readCacheToString(cache2);
    } finally {
        IOUtils.closeQuietly(cache1);
        IOUtils.closeQuietly(cache2);
        cache1.dispose();
        cache2.dispose();
    }
}

From source file:ch.cyberduck.core.cryptomator.CryptoChecksumComputeTest.java

@Test
public void testCompute() throws Exception {
    final Path vault = new Path("/vault", EnumSet.of(Path.Type.directory));
    final NullSession session = new NullSession(new Host(new TestProtocol())) {
        @Override//from   w w w  . j a  v a  2 s.com
        @SuppressWarnings("unchecked")
        public <T> T _getFeature(final Class<T> type) {
            if (type == Directory.class) {
                return (T) new Directory() {

                    @Override
                    public Path mkdir(final Path folder, final String region, final TransferStatus status)
                            throws BackgroundException {
                        assertTrue(folder.equals(vault) || folder.isChild(vault));
                        return folder;
                    }

                    @Override
                    public boolean isSupported(final Path workdir) {
                        return true;
                    }

                    @Override
                    public Directory withWriter(final Write writer) {
                        return this;
                    }
                };
            }
            return super._getFeature(type);
        }
    };
    final CryptoVault cryptomator = new CryptoVault(vault, new DisabledPasswordStore());
    cryptomator.create(session, null, new VaultCredentials("test"));
    final Cryptor cryptor = cryptomator.getCryptor();
    final ByteBuffer header = cryptor.fileHeaderCryptor().encryptHeader(cryptor.fileHeaderCryptor().create());
    // DEFAULT_PIPE_SIZE=1024
    final Path file = new Path(vault, "f", EnumSet.of(Path.Type.file));
    final SHA256ChecksumCompute sha = new SHA256ChecksumCompute();
    final CryptoChecksumCompute compute = new CryptoChecksumCompute(sha, cryptomator);
    final RandomNonceGenerator nonces = new RandomNonceGenerator();
    assertNotNull(compute.compute(new NullInputStream(1025L),
            new TransferStatus().withHeader(header).withNonces(nonces)).hash);
    assertNotEquals(
            compute.compute(new NullInputStream(1025L),
                    new TransferStatus().withHeader(header).withNonces(nonces)),
            compute.compute(new NullInputStream(1025L),
                    new TransferStatus().withHeader(header).withNonces(nonces)));
    assertNotNull(compute.compute(new NullInputStream(0L),
            new TransferStatus().withHeader(header).withNonces(nonces)).hash);
    final NullInputStream input = new NullInputStream(0L);
    assertEquals(compute.compute(input, new TransferStatus().withHeader(header).withNonces(nonces)),
            compute.compute(input, new TransferStatus().withHeader(header).withNonces(nonces)));
    assertNotEquals(
            compute.compute(new NullInputStream(0L),
                    new TransferStatus().withHeader(header).withNonces(nonces)),
            sha.compute(new NullInputStream(0L), new TransferStatus()));
}

From source file:com.talis.storage.SubmittedItemTest.java

@Test
public void settingMetadataInConstructorCreatesImmutableCopy() {
    Multimap<String, String> meta = ArrayListMultimap.create();
    meta.put("key-0", "value-0");
    meta.put("key-0", "value-1");
    meta.put("key-1", "value-3");

    SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_XML_TYPE, new NullInputStream(1024), meta);
    meta.put("key-2", "value-4");

    Multimap<String, String> fromItem = submittedItem.getMetadata();
    assertFalse(submittedItem.getMetadata().containsKey("key-2"));
}

From source file:com.norconex.committer.AbstractMappedCommitterTest.java

/**
 * Test setting the source and target IDs.
 * @throws IOException could not create temporary file
 *//*from w  w  w.ja va  2 s .c  om*/
@Test
public void testSetSourceAndTargetReference() throws IOException {

    // Set a different source and target id
    String customSourceId = "mysourceid";
    committer.setSourceReferenceField(customSourceId);
    String customTargetId = "mytargetid";
    committer.setTargetReferenceField(customTargetId);

    // Store the source id value in metadata
    metadata.addString(customSourceId, defaultReference);

    // Add a doc (it should trigger a commit because batch size is 1)
    committer.setQueueSize(1);
    committer.add(defaultReference, new NullInputStream(0), metadata);

    // Get the map generated
    assertEquals(1, committer.getCommitBatch().size());
    IAddOperation op = (IAddOperation) committer.getCommitBatch().get(0);
    Properties docMeta = op.getMetadata();

    // Check that customTargetId was used
    assertEquals(defaultReference, docMeta.getString(customTargetId));

    // Check that customSourceId was removed (default behavior)
    assertFalse("Source reference field was not removed.", docMeta.containsKey(customSourceId));
}

From source file:com.norconex.importer.handler.tagger.impl.DeleteTaggerTest.java

@Test
public void testDeleteFieldsViaXMLConfig() throws IOException, ImporterHandlerException {
    ImporterMetadata meta = new ImporterMetadata();
    meta.addString("content-type", "blah");
    meta.addString("x-access-level", "blah");
    meta.addString("X-CONTENT-TYPE-OPTIONS", "blah");
    meta.addString("X-FRAME-OPTIONS", "blah");
    meta.addString("X-PARSED-BY", "blah");
    meta.addString("date", "blah");
    meta.addString("X-RATE-LIMIT-LIMIT", "blah");
    meta.addString("source", "blah");

    DeleteTagger tagger = new DeleteTagger();

    Reader r = new StringReader("<tagger><fields>X-ACCESS-LEVEL,X-content-type-options,"
            + "X-FRAME-OPTIONS,X-PARSED-BY,X-RATE-LIMIT-LIMIT</fields>" + "</tagger>");
    tagger.loadFromXML(r);/* w w w .j a v  a  2 s  .co m*/

    tagger.tagDocument("blah", new NullInputStream(0), meta, false);

    Assert.assertEquals("Invalid field count", 3, meta.size());
}

From source file:com.norconex.importer.handler.tagger.impl.KeepOnlyTaggerTest.java

@Test
public void testKeepFieldsRegexViaXMLConfig() throws IOException, ImporterHandlerException {
    ImporterMetadata meta = new ImporterMetadata();
    meta.addString("content-type", "blah");
    meta.addString("x-access-level", "blah");
    meta.addString("X-CONTENT-TYPE-OPTIONS", "blah");
    meta.addString("X-FRAME-OPTIONS", "blah");
    meta.addString("X-PARSED-BY", "blah");
    meta.addString("date", "blah");
    meta.addString("X-RATE-LIMIT-LIMIT", "blah");
    meta.addString("source", "blah");

    KeepOnlyTagger tagger = new KeepOnlyTagger();

    Reader r = new StringReader("<tagger><fieldsRegex>[Xx]-.*</fieldsRegex></tagger>");
    tagger.loadFromXML(r);//from   w w w.  ja v  a  2s . c  o  m

    tagger.tagDocument("blah", new NullInputStream(0), meta, false);

    Assert.assertEquals("Invalid field count", 5, meta.size());
}

From source file:ch.cyberduck.core.PathTest.java

@Test
public void testTransfer() throws Exception {
    Path p = new NullPath("/t", Path.FILE_TYPE);
    final TransferStatus status = new TransferStatus();
    status.setLength(432768L);//  w w w . ja  va2  s. c  o  m
    p.transfer(new NullInputStream(status.getLength()), new NullOutputStream(), new StreamListener() {
        long sent;
        long received;

        @Override
        public void bytesSent(long bytes) {
            assertTrue(bytes > 0L);
            assertTrue(bytes <= 32768L);
            sent += bytes;
            assertTrue(sent == received);
        }

        @Override
        public void bytesReceived(long bytes) {
            assertTrue(bytes > 0L);
            assertTrue(bytes <= 32768L);
            received += bytes;
            assertTrue(received > sent);
        }
    }, -1, status);
    assertTrue(status.isComplete());
    assertTrue(status.getCurrent() == status.getLength());
}

From source file:com.nesscomputing.httpclient.response.ContentResponseHandler.java

/**
 * Processes the client response./*from  www. j  a  va 2  s. com*/
 */
@Override
public T handle(final HttpClientResponse response) throws IOException {
    if (allowRedirect && response.isRedirected()) {
        LOG.debug("Redirecting based on '%d' response code", response.getStatusCode());
        throw new RedirectedException(response);
    } else {
        // Find the response stream - the error stream may be valid in cases
        // where the input stream is not.
        InputStream is = null;
        try {
            is = response.getResponseBodyAsStream();
        } catch (IOException e) {
            LOG.warnDebug(e, "Could not locate response body stream");
            // normal for 401, 403 and 404 responses, for example...
        }

        if (is == null) {
            // Fall back to zero length response.
            is = new NullInputStream(0);
        }

        try {
            final Long contentLength = response.getContentLength();

            if (maxBodyLength > 0) {
                if (contentLength != null && contentLength > maxBodyLength) {
                    throw new SizeExceededException("Content-Length: " + contentLength);
                }

                LOG.debug("Limiting stream length to '%d'", maxBodyLength);
                is = new SizeLimitingInputStream(is, maxBodyLength);
            }

            final String encoding = StringUtils.trimToEmpty(response.getHeader("Content-Encoding"));

            if (StringUtils.equalsIgnoreCase(encoding, "lz4")) {
                LOG.debug("Found LZ4 stream");
                is = new LZ4BlockInputStream(is);
            } else if (StringUtils.equalsIgnoreCase(encoding, "gzip")
                    || StringUtils.equalsIgnoreCase(encoding, "x-gzip")) {
                LOG.debug("Found GZIP stream");
                is = new GZIPInputStream(is);
            } else if (StringUtils.equalsIgnoreCase(encoding, "deflate")) {
                LOG.debug("Found deflate stream");
                final Inflater inflater = new Inflater(true);
                is = new InflaterInputStream(is, inflater);
            }

            return contentConverter.convert(response, is);
        } catch (IOException ioe) {
            return contentConverter.handleError(response, ioe);
        }
    }
}

From source file:com.norconex.committer.AbstractMappedCommitterTest.java

/**
 * Test keeping source id field./*from w ww . j  a  v  a  2 s. c o m*/
 * @throws IOException could not create temporary file 
 */
@Test
public void testKeepSourceId() throws IOException {

    committer.setKeepSourceReferenceField(true);

    // Add a doc (it should trigger a commit because batch size is 1)
    committer.setQueueSize(1);
    committer.add(defaultReference, new NullInputStream(0), metadata);
    committer.commit();

    // Get the map generated
    assertEquals(1, committer.getCommitBatch().size());
    IAddOperation op = (IAddOperation) committer.getCommitBatch().get(0);

    // Check that the source id is still there
    assertTrue(op.getMetadata().containsKey("myreference"));
}