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:org.kitodo.filemanagement.locking.LockManagementTest.java

/**
 * Multiple users can get an upgradeable read lock on a file, but only one user
 * at a time can expand its read lock for a one-time write. As part of the
 * contract, the user who wants to rewrite the file must read it first. While at
 * least one user has an extensible read lock on a URI, no user can get
 * exclusive access to the URI. If he tries, he gets back the names of the lock
 * owners./*  w  w w.  j  av a2 s . c om*/
 */
@Test
public void testUpgradeableReadLocking() throws IOException {
    underTest.clear();

    LockResult alicesAccess = underTest.tryLock(createRequest(ALICE, AN_URI, LockingMode.UPGRADEABLE_READ),
            null);
    assertTrue(MESSAGE_ALICE_ALLOWED, alicesAccess instanceof GrantedAccess);

    LockResult bobsAccess = underTest.tryLock(createRequest(BOB, AN_URI, LockingMode.UPGRADEABLE_READ), null);
    assertTrue(MESSAGE_BOB_ALLOWED, bobsAccess instanceof GrantedAccess);

    Map<URI, Collection<String>> noConflictForAlice = alicesAccess
            .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE));
    assertTrue(MESSAGE_ALICE_LOCK, noConflictForAlice.entrySet().isEmpty());

    Map<URI, Collection<String>> bobsConflict = bobsAccess
            .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE));
    assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty());
    assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE));

    try (InputStream aliceIsReading = underTest.reportGrant(AN_URI, new NullInputStream(0), alicesAccess)) {
        bobsConflict = bobsAccess.tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE));
        assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty());
        assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE));
    }

    bobsConflict = bobsAccess.tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE));
    assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty());
    assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE));

    try (OutputStream aliceIsWriting = underTest.reportGrant(AN_URI, new NullOutputStream(), alicesAccess)) {
        bobsConflict = bobsAccess.tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE));
        assertFalse(MESSAGE_BOB_LOCK, bobsConflict.entrySet().isEmpty());
        assertTrue(MESSAGE_PROBLEM, bobsConflict.get(AN_URI).contains(ALICE));
    }

    Map<URI, Collection<String>> bobsMove = bobsAccess
            .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE));
    assertTrue("Bob should have been able to extend his lock", bobsMove.entrySet().isEmpty());

    Map<URI, Collection<String>> alicesConflict = alicesAccess
            .tryLock(createRequest(AN_URI, LockingMode.UPGRADE_WRITE_ONCE));
    assertFalse("Alice should not have been able to extend her lock", alicesConflict.entrySet().isEmpty());
    assertTrue("Alice should have learned that Bob is her problem", alicesConflict.get(AN_URI).contains(BOB));
}

From source file:org.kitodo.filemanagement.locking.LockManagementTest.java

/**
 * Mimic the lock management that the file would be read. The lock management
 * generates a stream guard for the transferred stream. When the stream guard is
 * closed, the lock management believes the file was read.
 *
 * @param lockManagement/*from   w  w  w  .j av  a 2 s.  c o  m*/
 *            lock management to fool
 * @param uri
 *            URI of stream
 * @param access
 *            granted access
 */
private static void mimicReading(LockManagement lockManagement, URI uri, GrantedAccess access)
        throws IOException {
    lockManagement.reportGrant(uri, new NullInputStream(0), access).close();
}

From source file:org.lilyproject.rest.BlobCollectionResource.java

@POST
@Consumes("*/*")//w  w w  . j  ava 2 s  .c  o m
@Produces("application/json")
public Response post(@Context HttpHeaders headers, @Context UriInfo uriInfo, InputStream is) {
    String lengthHeader = headers.getRequestHeaders().getFirst(HttpHeaders.CONTENT_LENGTH);
    if (lengthHeader == null) {
        throw new ResourceException("Content-Length header is required for uploading blobs.",
                BAD_REQUEST.getStatusCode());
    }

    // TODO do we want the mediatype to include the parameters?
    String mediaType = headers.getMediaType().getType() + "/" + headers.getMediaType().getSubtype();

    long length = Long.parseLong(lengthHeader);
    Blob blob = new Blob(mediaType, length, null);

    if (length == 0 && is == null) {
        // Apparently when the length is 0, no InputStream is provided, therefore the following
        is = new NullInputStream(0);
    }

    OutputStream os = null;
    try {
        os = getTable(uriInfo).getOutputStream(blob);
        IOUtils.copyLarge(is, os);
    } catch (Exception e) {
        throw new ResourceException("Error writing blob.", e, INTERNAL_SERVER_ERROR.getStatusCode());
    } finally {
        Closer.close(os);
    }

    return Response.ok().entity(blob).build();
}

From source file:org.lilyproject.solrtestfw.SolrHomeDirSetup.java

private void createEmptyFile(File destination) throws IOException {
    FileUtils.copyInputStreamToFile(new NullInputStream(0), destination);
}

From source file:org.mule.module.s3.S3TestCase.java

@Test
public void createObjectSimple() {
    PutObjectRequest request = new PutObjectRequest(MY_BUCKET, MY_OBJECT, new NullInputStream(0),
            new ObjectMetadata());
    request.setCannedAcl(CannedAccessControlList.Private);
    request.setStorageClass(StorageClass.ReducedRedundancy);
    when(client.putObject(refEq(request, "metadata", "inputStream"))).thenReturn(new PutObjectResult());

    assertNull(connector.createObject(MY_BUCKET, MY_OBJECT, "have a nice release", null, null, null, PRIVATE,
            org.mule.module.s3.StorageClass.REDUCED_REDUNDANCY, null));
}

From source file:org.mule.module.s3.S3TestCase.java

@Test
public void createObjectInputStreamParameter() {
    long contentLength = 100L;
    when(client.putObject(argThat(new ContentMetadataMatcher(contentLength, "A5B69...", "text/plain"))))
            .thenReturn(new PutObjectResult());
    assertNull(connector.createObject(MY_BUCKET, MY_OBJECT, new NullInputStream(0), contentLength, "A5B69...",
            "text/plain", PUBLIC_READ_WRITE, org.mule.module.s3.StorageClass.STANDARD, null));
}

From source file:org.mule.module.s3.S3TestCase.java

@Test
public void createObjectWithFullOptions() throws Exception {
    PutObjectRequest request = new PutObjectRequest(MY_BUCKET, MY_OBJECT, new NullInputStream(0),
            new ObjectMetadata());
    request.setCannedAcl(CannedAccessControlList.PublicRead);
    request.setStorageClass(StorageClass.Standard);
    when(client.putObject(refEq(request, "metadata", "inputStream"))).thenReturn(new PutObjectResult());
    assertNull(connector.createObject(MY_BUCKET, MY_OBJECT, "have a nice release", null, null, "text/plain",
            PUBLIC_READ, org.mule.module.s3.StorageClass.STANDARD, null));
}

From source file:org.mule.module.s3.S3TestCase.java

@Test
public void getObjectContent() throws Exception {
    S3Object s3Object = new S3Object();
    NullInputStream content = new NullInputStream(0);
    s3Object.setObjectContent(content);

    when(client.getObject(refEq(new GetObjectRequest(MY_BUCKET, MY_OBJECT)))).thenReturn(s3Object);
    assertSame(content, connector.getObjectContent(MY_BUCKET, MY_OBJECT, null, null, null));
}

From source file:org.mule.module.s3.S3TestCase.java

@Test
public void getObjectContentWithVersion() throws Exception {
    S3Object s3Object = new S3Object();
    NullInputStream content = new NullInputStream(0);
    s3Object.setObjectContent(content);

    when(client.getObject(refEq(new GetObjectRequest(MY_BUCKET, MY_OBJECT, "9")))).thenReturn(s3Object);
    assertSame(content, connector.getObjectContent(MY_BUCKET, MY_OBJECT, "9", null, null));
}

From source file:org.nuxeo.ecm.core.opencmis.impl.server.NuxeoContentStream.java

private NuxeoContentStream(Blob blob, GregorianCalendar lastModified, boolean isHeadRequest) {
    this.blob = blob;
    this.lastModified = lastModified;
    // The callers of getStream() often just want to know if the stream is null or not.
    // (Callers are ObjectService.GetContentStream / AbstractServiceCall.sendContentStreamHeaders)
    // Also in case we end up redirecting, we don't want to get the stream (which is possibly costly) to just have
    // it closed immediately. So we wrap in a lazy implementation
    if (isHeadRequest) {
        stream = new NullInputStream(0);
    } else {/*from www.  jav  a2 s . com*/
        stream = new LazyInputStream(this::getActualStream);
    }
}