Java源码示例:org.apache.commons.io.input.NullInputStream

示例1
@Test
public void testWriteZeroLength() throws Exception {
    final StoregateIdProvider nodeid = new StoregateIdProvider(session).withCache(cache);
    final Path room = new StoregateDirectoryFeature(session, nodeid).mkdir(
        new Path(String.format("/My files/%s", new AlphanumericRandomStringService().random()),
            EnumSet.of(Path.Type.directory, Path.Type.volume)), null, new TransferStatus());
    final TransferStatus status = new TransferStatus();
    final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final StoregateMultipartWriteFeature writer = new StoregateMultipartWriteFeature(session, nodeid);
    final HttpResponseOutputStream<VersionId> out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new NullInputStream(0L), out);
    final VersionId version = out.getStatus();
    assertNotNull(version);
    assertTrue(new DefaultFindFeature(session).find(test));
    new StoregateDeleteFeature(session, nodeid).delete(Collections.singletonList(room), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
示例2
@Test
public void testWriteZeroLength() throws Exception {
    final SDSNodeIdProvider nodeid = new SDSNodeIdProvider(session).withCache(cache);
    final Path room = new SDSDirectoryFeature(session, nodeid).mkdir(
        new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory, Path.Type.volume, Path.Type.triplecrypt)), null, new TransferStatus());
    final TransferStatus status = new TransferStatus();
    final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final SDSMultipartWriteFeature writer = new SDSMultipartWriteFeature(session, nodeid);
    final HttpResponseOutputStream<VersionId> out = writer.write(test, status, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(status, status).transfer(new NullInputStream(0L), out);
    final VersionId version = out.getStatus();
    assertNotNull(version);
    assertTrue(new DefaultFindFeature(session).find(test));
    new SDSDeleteFeature(session, nodeid).delete(Collections.singletonList(room), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
示例3
@Override
public Path mkdir(final Path folder, final String region, final TransferStatus status) throws BackgroundException {
    if(containerService.isContainer(folder)) {
        final S3BucketCreateService service = new S3BucketCreateService(session);
        service.create(folder, StringUtils.isBlank(region) ? PreferencesFactory.get().getProperty("s3.location") : region);
        return folder;
    }
    else {
        status.setChecksum(writer.checksum(folder, status).compute(new NullInputStream(0L), status));
        // Add placeholder object
        status.setMime(MIMETYPE);
        final EnumSet<Path.Type> type = EnumSet.copyOf(folder.getType());
        type.add(Path.Type.placeholder);
        final StatusOutputStream<StorageObject> out = writer.write(new Path(folder.getParent(), folder.getName(), type,
            new PathAttributes(folder.attributes())), status, new DisabledConnectionCallback());
        new DefaultStreamCloser().close(out);
        final StorageObject metadata = out.getStatus();
        return new Path(folder.getParent(), folder.getName(), type,
            new S3AttributesFinderFeature(session).toAttributes(metadata));
    }
}
 
示例4
@Override
public synchronized int read(final byte[] chunk, final Long offset) throws IOException {
    final RandomAccessFile file = random();
    if(offset < file.length()) {
        file.seek(offset);
        if(chunk.length + offset > file.length()) {
            return file.read(chunk, 0, (int) (file.length() - offset));
        }
        else {
            return file.read(chunk, 0, chunk.length);
        }
    }
    else {
        final NullInputStream nullStream = new NullInputStream(length);
        if(nullStream.available() > 0) {
            nullStream.skip(offset);
            return nullStream.read(chunk, 0, chunk.length);
        }
        else {
            return IOUtils.EOF;
        }
    }
}
 
示例5
protected ServletInputStream bodyStringToInputStream(String body, boolean isBase64Encoded) throws IOException {
    if (body == null) {
        return new AwsServletInputStream(new NullInputStream(0, false, false));
    }
    byte[] bodyBytes;
    if (isBase64Encoded) {
        bodyBytes = Base64.getMimeDecoder().decode(body);
    } else {
        String encoding = getCharacterEncoding();
        if (encoding == null) {
            encoding = StandardCharsets.ISO_8859_1.name();
        }
        try {
            bodyBytes = body.getBytes(encoding);
        } catch (Exception e) {
            log.error("Could not read request with character encoding: " + SecurityUtils.crlf(encoding), e);
            bodyBytes = body.getBytes(StandardCharsets.ISO_8859_1.name());
        }
    }
    ByteArrayInputStream requestBodyStream = new ByteArrayInputStream(bodyBytes);
    return new AwsServletInputStream(requestBodyStream);
}
 
示例6
@Before
public void setUp() throws Exception {
    releasableInputStream = new ReleasableInputStream(new NullInputStream(2048), () -> wasCalled.set(true));
    failedReleasableInputStream = new ReleasableInputStream(new InputStream() {

        @Override
        public int read() throws IOException {
            throw new IOException("Oops");
        }

        @Override
        public int available() throws IOException {
            throw new IOException("Oops");
        }

        @Override
        public synchronized void mark(int readlimit) {
            throw new RuntimeException("Oops");
        }
    }, () -> wasCalled.set(true));
}
 
示例7
public void testWriteGiantRecord() throws IOException {
    PrintStream dummyStream = new PrintStream(new NullOutputStream());
    ARCWriter arcWriter = 
        new ARCWriter(
                SERIAL_NO,
                dummyStream,
                new File("dummy"),
                new WriterPoolSettingsData(
                        "", 
                        "", 
                        -1, 
                        false, 
                        null, 
                        null));
    assertNotNull(arcWriter);

    // Start the record with an arbitrary 14-digit date per RFC2540
    long now = System.currentTimeMillis();
    long recordLength = org.apache.commons.io.FileUtils.ONE_GB * 3;
   
    arcWriter.write("dummy:uri", "application/octet-stream",
        "0.1.2.3", now, recordLength, new NullInputStream(recordLength));
    arcWriter.close();
}
 
示例8
@Test
public void testUploadFindingsPositiveCase() throws Exception {
    String token = "db975c97-98b1-4988-8d6a-9c3e044dfff3";
    String applicationVersion = "12345";
    new MockServerClient("localhost", 1080)
            .when(
                    request()
                            .withMethod("POST")
                            .withHeader(HttpHeaders.ACCEPT, "application/xml")
                            .withPath("/ssc/upload/resultFileUpload.html?mat=" + token + "&engineType=DEPENDENCY_TRACK&entityId=" + applicationVersion)
                            .withQueryStringParameter("engineType", "DEPENDENCY_TRACK")
                            .withQueryStringParameter("mat", token)
                            .withQueryStringParameter("entityId", applicationVersion)
            )
            .respond(
                    response()
                            .withStatusCode(200)
                            .withHeader(HttpHeaders.CONTENT_TYPE, "application/xml")
            );
    FortifySscUploader uploader = new FortifySscUploader();
    FortifySscClient client = new FortifySscClient(uploader, new URL("https://localhost/ssc"));
    client.uploadDependencyTrackFindings(token, applicationVersion, new NullInputStream(0));
}
 
示例9
@Test
public void testUploadFindingsNegativeCase() throws Exception {
    String token = "db975c97-98b1-4988-8d6a-9c3e044dfff3";
    String applicationVersion = "";
    new MockServerClient("localhost", 1080)
            .when(
                    request()
                            .withMethod("POST")
                            .withHeader(HttpHeaders.ACCEPT, "application/xml")
                            .withPath("/ssc/upload/resultFileUpload.html?mat=" + token + "&engineType=DEPENDENCY_TRACK&entityId=" + applicationVersion)
                            .withQueryStringParameter("engineType", "DEPENDENCY_TRACK")
                            .withQueryStringParameter("mat", token)
                            .withQueryStringParameter("entityId", applicationVersion)
            )
            .respond(
                    response()
                            .withStatusCode(400)
                            .withHeader(HttpHeaders.CONTENT_TYPE, "application/xml")
            );
    FortifySscUploader uploader = new FortifySscUploader();
    FortifySscClient client = new FortifySscClient(uploader, new URL("https://localhost/ssc"));
    client.uploadDependencyTrackFindings(token, applicationVersion, new NullInputStream(16));
}
 
示例10
@Override
public Path mkdir(final Path folder, final String region, final TransferStatus status) throws BackgroundException {
    if(containerService.isContainer(folder)) {
        return super.mkdir(folder, region, status);
    }
    else {
        status.setChecksum(writer.checksum(folder, status).compute(new NullInputStream(0L), status));
        return super.mkdir(folder, region, status);
    }
}
 
示例11
@Override
public Path touch(final Path file, final TransferStatus status) throws BackgroundException {
    status.setChecksum(writer.checksum(file, status).compute(new NullInputStream(0L), status));
    status.setTimestamp(System.currentTimeMillis());
    final StatusOutputStream<BaseB2Response> out = writer.write(file, status, new DisabledConnectionCallback());
    new DefaultStreamCloser().close(out);
    return new Path(file.getParent(), file.getName(), file.getType(),
        new B2AttributesFinderFeature(session, fileid).toAttributes((B2FileResponse) out.getStatus()));
}
 
示例12
@Override
public Checksum compute(final InputStream in, final TransferStatus status) throws ChecksumException {
    if(Checksum.NONE == delegate.compute(new NullInputStream(0L), new TransferStatus())) {
        return Checksum.NONE;
    }
    if(null == status.getHeader()) {
        // Write header to be reused in writer
        final FileHeader header = cryptomator.getFileHeaderCryptor().create();
        status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
    }
    // Make nonces reusable in case we need to compute a checksum
    status.setNonces(new RotatingNonceGenerator(cryptomator.numberOfChunks(status.getLength())));
    return this.compute(this.normalize(in, status), status.getOffset(), status.getHeader(), status.getNonces());
}
 
示例13
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        if(file.getType().contains(Path.Type.upload)) {
            return new NullInputStream(0L);
        }
        final HttpRange range = HttpRange.withStatus(status);
        final RequestEntityRestStorageService client = session.getClient();
        final S3Object object = client.getVersionedObject(
            file.attributes().getVersionId(),
            containerService.getContainer(file).getName(),
            containerService.getKey(file),
            null, // ifModifiedSince
            null, // ifUnmodifiedSince
            null, // ifMatch
            null, // ifNoneMatch
            status.isAppend() ? range.getStart() : null,
            status.isAppend() ? (range.getEnd() == -1 ? null : range.getEnd()) : null);
        if(log.isDebugEnabled()) {
            log.debug(String.format("Reading stream with content length %d", object.getContentLength()));
        }
        return object.getDataInputStream();
    }
    catch(ServiceException e) {
        throw new S3ExceptionMappingService().map("Download {0} failed", e, file);
    }
}
 
示例14
@Override
public Path touch(final Path file, final TransferStatus status) throws BackgroundException {
    status.setChecksum(writer.checksum(file, status).compute(new NullInputStream(0L), status));
    status.setLength(0L);
    final StatusOutputStream<StorageObject> out = writer.write(file, status, new DisabledConnectionCallback());
    new DefaultStreamCloser().close(out);
    final S3Object metadata = (S3Object) out.getStatus();
    return new Path(file.getParent(), file.getName(), file.getType(),
        new S3AttributesFinderFeature(session).toAttributes(metadata));
}
 
示例15
@Test
public void testDecorate() throws Exception {
    final NullInputStream n = new NullInputStream(1L);
    final S3Session session = new S3Session(new Host(new S3Protocol()));
    assertSame(NullInputStream.class, new S3SingleUploadService(session,
        new S3WriteFeature(session, new S3DisabledMultipartService())).decorate(n, null).getClass());
}
 
示例16
@Test(expected = IOException.class)
public void compute() throws Exception {
    final NullInputStream in = new NullInputStream(0L);
    new DisabledChecksumCompute().compute(in, new TransferStatus());
    assertEquals(-1, in.read());
    in.read();
}
 
示例17
@Test
public void testComputeEmptyString() throws Exception {
    assertEquals("d41d8cd98f00b204e9800998ecf8427e",
        new MD5ChecksumCompute().compute(IOUtils.toInputStream("", Charset.defaultCharset()), new TransferStatus()).hash);
    assertEquals("d41d8cd98f00b204e9800998ecf8427e",
        new MD5ChecksumCompute().compute(new NullInputStream(0L), new TransferStatus().length(0)).hash);
}
 
示例18
@Test
public void testTransferFixedLength() throws Exception {
    final TransferStatus status = new TransferStatus().length(432768L);
    new StreamCopier(status, status).withLimit(432768L).transfer(new NullInputStream(432768L), new NullOutputStream());
    assertTrue(status.isComplete());
    assertEquals(432768L, status.getOffset(), 0L);
}
 
示例19
@Test
public void testTransferFixedLengthIncomplete() throws Exception {
    final TransferStatus status = new TransferStatus().length(432768L);
    new StreamCopier(status, status).withLimit(432767L).transfer(new NullInputStream(432768L), new NullOutputStream());
    assertEquals(432767L, status.getOffset(), 0L);
    assertTrue(status.isComplete());
}
 
示例20
@Test
public void testReadNoEndofStream() throws Exception {
    final TransferStatus status = new TransferStatus().length(432768L);
    new StreamCopier(status, status).withLimit(432768L).transfer(new NullInputStream(432770L), new NullOutputStream());
    assertEquals(432768L, status.getOffset(), 0L);
    assertTrue(status.isComplete());
}
 
示例21
@Test
public void testCompute() throws Exception {
    assertEquals("0",
            new CRC32ChecksumCompute().compute(new NullInputStream(0), new TransferStatus()).hash);
    assertEquals("d202ef8d",
            new CRC32ChecksumCompute().compute(new NullInputStream(1L), new TransferStatus()).hash);
}
 
示例22
@Test
public void testComputeEmptyString() throws Exception {
    assertEquals("d41d8cd98f00b204e9800998ecf8427e",
        new MD5FastChecksumCompute().compute(IOUtils.toInputStream("", Charset.defaultCharset()), new TransferStatus()).hash);
    assertEquals("d41d8cd98f00b204e9800998ecf8427e",
        new MD5FastChecksumCompute().compute(new NullInputStream(0L), new TransferStatus().length(0)).hash);
}
 
示例23
@Override
public int read()
        throws IOException {
    if (bodyStream == null || bodyStream instanceof NullInputStream) {
        return -1;
    }
    int readByte = bodyStream.read();
    if (readByte == -1) {
        finished = true;
    }
    return readByte;
}
 
示例24
@Override
public InputStream openStream()
    throws IOException
{
    try
    {
        return file.getInputStream();
    }
    catch ( IOException ioe )
    {
        return new NullInputStream( 0 );
    }
}
 
示例25
@Override
public InputStream openStream()
    throws IOException
{
    try
    {
        return file.getInputStream();
    }
    catch ( IOException ioe )
    {
        return new NullInputStream( 0 );
    }
}
 
示例26
/**
 * Checks if a file exists in the zip
 *
 * @param payload  zip file as a payload
 * @param path     path of stream to be extracted
 * @param fileName file to check exists
 * @return true if it exists
 */
public boolean fileExists(final Payload payload, final String path, final String fileName) {
  try (InputStream projectAsStream = payload.openInputStream()) {
    return extract(projectAsStream, fileName, (ZipInputStream z) -> new NullInputStream(-1)) != null;
  }
  catch (IOException e) {
    log.warn("Unable to open content {}", path, e);
  }
  return false;
}
 
示例27
@Test
public void testDecorate() throws Exception {
    final NullInputStream n = new NullInputStream(1L);
    assertSame(NullInputStream.class, new SwiftSmallObjectUploadFeature(new SwiftWriteFeature(
        session, new SwiftRegionService(session))).decorate(n, null).getClass());
}
 
示例28
public CommonsHttpResponse(final HttpResponse response) throws IOException {
    super(null == response.getEntity() ? new NullInputStream(0L) :
        new HttpMethodReleaseInputStream(response));
    this.response = response;
}
 
示例29
@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
        @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) {
                        assertTrue(folder.equals(vault) || folder.isChild(vault));
                        return folder;
                    }

                    @Override
                    public Directory withWriter(final Write writer) {
                        return this;
                    }
                };
            }
            return super._getFeature(type);
        }
    };
    final CryptoVault cryptomator = new CryptoVault(vault);
    cryptomator.create(session, null, new VaultCredentials("test"), new DisabledPasswordStore());
    final ByteBuffer header = cryptomator.getFileHeaderCryptor().encryptHeader(cryptomator.getFileHeaderCryptor().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()));
}
 
示例30
@Override
public Path touch(final Path file, final TransferStatus status) throws BackgroundException {
    status.setChecksum(writer.checksum(file, status).compute(new NullInputStream(0L), status));
    new DefaultStreamCloser().close(writer.write(file, status, new DisabledConnectionCallback()));
    return new Path(file.getParent(), file.getName(), file.getType(), new AzureAttributesFinderFeature(session, context).find(file));
}