Example usage for java.io InputStream InputStream

List of usage examples for java.io InputStream InputStream

Introduction

In this page you can find the example usage for java.io InputStream InputStream.

Prototype

InputStream

Source Link

Usage

From source file:com.github.davidcarboni.encryptedfileupload.StreamingTest.java

/**
 * Test for FILEUPLOAD-135/* ww  w.  j a  v  a2  s  .  c o m*/
 */
public void testFILEUPLOAD135() throws IOException, FileUploadException {
    byte[] request = newShortRequest();
    final ByteArrayInputStream bais = new ByteArrayInputStream(request);
    List<FileItem> fileItems = parseUpload(new InputStream() {
        @Override
        public int read() throws IOException {
            return bais.read();
        }

        @Override
        public int read(byte b[], int off, int len) throws IOException {
            return bais.read(b, off, Math.min(len, 3));
        }

    }, request.length);
    Iterator<FileItem> fileIter = fileItems.iterator();
    assertTrue(fileIter.hasNext());
    FileItem item = fileIter.next();
    assertEquals("field", item.getFieldName());
    byte[] bytes = item.get();
    assertEquals(3, bytes.length);
    assertEquals((byte) '1', bytes[0]);
    assertEquals((byte) '2', bytes[1]);
    assertEquals((byte) '3', bytes[2]);
    assertTrue(!fileIter.hasNext());
}

From source file:ddf.platform.scheduler.CommandJob.java

private void doExecute(final String commandString) {
    final SessionFactory sessionFactory = getSessionFactory();
    if (sessionFactory != null) {
        try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                final PrintStream output = createPrintStream(byteArrayOutputStream);
                // TODO DDF-3280 remove work-around for NPE when creating session with a null "in"
                // parameter from a SessionFactory
                final InputStream emptyInputStream = new InputStream() {
                    @Override//w  w  w  .  j  a  v  a2  s.  co  m
                    public int read() throws IOException {
                        LOGGER.error(
                                "This method implementation of an InputStream is just a work-around for a Karaf bug and should never be called. There is an issue with the Platform Command Scheduler implementation.");
                        return -1;
                    }
                };
                final Session session = sessionFactory.create(emptyInputStream, output, output)) {
            if (session != null) {
                LOGGER.trace("Executing command \"{}\"", commandString);
                try {
                    session.execute(commandString);

                    try {
                        final String commandOutput = byteArrayOutputStream
                                .toString(StandardCharsets.UTF_8.name());

                        LOGGER.info("Execution output for command \"{}\": {}", commandString, commandOutput);
                    } catch (UnsupportedEncodingException e) {
                        LOGGER.debug("Unable to get command output.", e);
                    }
                } catch (Exception e) {
                    logWarningMessage(commandString);
                    LOGGER.debug("Unable to execute command.", e);
                }
            } else {
                logWarningMessage(commandString);
                LOGGER.debug("Unable to create session.");
            }
        } catch (IOException e) {
            logWarningMessage(commandString);
            LOGGER.debug("Unable to create session.", e);
        }
    } else {
        logWarningMessage(commandString);
        LOGGER.debug("Unable to create session factory.");
    }
}

From source file:org.apache.hadoop.fs.oss.ExtraOSSAPIsTest.java

/**
 * InputStream code coverage test./*from w w  w .  ja v  a2 s  . c om*/
 *
 * @throws Exception
 */
public void testOSSInputStream() throws Exception {

    final InputStream nullStream = new InputStream() {
        @Override
        public int read() throws IOException {
            return -1;
        }
    };
    client.putObject("hadoop-intg", "test/test-oss-inputstream", nullStream);
    FSDataInputStream inputStream = fileSystem.open(path("/test/test-oss-inputstream"), 100);

    assertEquals(inputStream.available(), 0);
    assertEquals(inputStream.markSupported(), false);
}

From source file:org.apache.jackrabbit.oak.spi.blob.AbstractBlobStoreTest.java

@Test
public void testExceptionWhileReading() throws Exception {
    final AtomicBoolean closed = new AtomicBoolean();
    InputStream in = new InputStream() {
        @Override/*from w ww. ja va2  s .  c o m*/
        public void close() {
            closed.set(true);
        }

        @Override
        public int read() throws IOException {
            throw new RuntimeException("abc");
        }
    };
    try {
        store.writeBlob(in);
    } catch (Exception e) {
        String msg = e.getMessage();
        assertTrue(msg, msg.indexOf("abc") >= 0);
    }
    assertTrue(closed.get());
}

From source file:org.apache.jackrabbit.core.config.RepositoryConfigTest.java

/**
 * Tests that an input stream can be used for the configuration.
 *///from   w ww .  jav  a 2s  .  c  o  m
public void testRepositoryConfigCreateWithInputStream() throws IOException {
    InputStream input = new FileInputStream(XML);
    try {
        RepositoryConfig.create(input, DIR.getPath());
    } catch (ConfigurationException e) {
        fail("Valid configuration input stream");
    } finally {
        input.close();
    }

    try {
        RepositoryConfig.create(new InputStream() {
            public int read() throws IOException {
                throw new IOException("invalid input stream");
            }
        }, DIR.getPath());
        fail("Invalid configuration input stream");
    } catch (ConfigurationException e) {
    }

    try {
        RepositoryConfig.create(new ClosedInputStream(), DIR.getPath());
        fail("Invalid configuration input stream");
    } catch (ConfigurationException e) {
    }
}

From source file:com.proofpoint.jaxrs.AbstractMapperTest.java

@Test(expectedExceptions = IOException.class)
public void testOtherIOExceptionThrowsIOException() throws IOException {
    try {/*from   w  ww.  j  a va  2  s .co  m*/
        mapper.readFrom(Object.class, Object.class, null, null, null, new InputStream() {
            @Override
            public int read() throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b) throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                throw new ZipException("forced ZipException");
            }
        });
        fail("Should have thrown an IOException");
    } catch (WebApplicationException e) {
        fail("Should not have received an IOException", e);
    }
}

From source file:org.eclipse.smila.binarystorage.persistence.io.test.TestBinaryPersistence.java

/**
 * Test exception handle logic.//w  w  w.j a  v  a  2 s. c o  m
 *
 * @throws Exception
 *           the exception
 */
public void testExceptions() throws Exception {
    final BinaryStorageConfiguration configuration = prepareConfiguration();
    final BinaryPersistence binaryPersistence = BinaryPersistenceFactory.newImplInstance(configuration);

    try {
        binaryPersistence.storeBinary(KEY, new InputStream() {
            @Override
            public int read() throws IOException {
                throw new IOException("Test IOException");
            }
        });
        fail("Must throw BinaryStorageException");
    } catch (final BinaryStorageException e) {
        assertEquals("Test IOException", e.getRootCause().getMessage());
        ; // ok
    }

    final File tempFile = getTempExistingFile();
    FileInputStream fileInputStream = new FileInputStream(tempFile) {
        @Override
        public FileChannel getChannel() {
            return new DummyFileChannel() {
                @Override
                public long size() throws IOException {
                    throw new IOException("Test IOException");
                }
            };
        }
    };
    try {
        binaryPersistence.storeBinary(KEY, fileInputStream);
        fail("Must throw BinaryStorageException");
    } catch (final BinaryStorageException e) {
        assertEquals("Test IOException", e.getRootCause().getMessage());
    }

    fileInputStream = new FileInputStream(tempFile) {
        @Override
        public FileChannel getChannel() {
            return new DummyFileChannel() {
                @Override
                public long size() throws IOException {
                    return FILE_SIZE_64MB + 1;
                }

                @Override
                public long transferTo(final long position, final long count, final WritableByteChannel target)
                        throws IOException {
                    throw new IOException("Test IOException");
                }
            };
        }
    };
    try {
        binaryPersistence.storeBinary(KEY, fileInputStream);
        fail("Must throw BinaryStorageException");
    } catch (final BinaryStorageException e) {
        assertEquals("Test IOException", e.getRootCause().getMessage());
    }

    fileInputStream = new FileInputStream(tempFile) {
        @Override
        public FileChannel getChannel() {
            return new DummyFileChannel() {
                @Override
                protected void implCloseChannel() throws IOException {
                    throw new IOException("Test IOException");
                }
            };
        }
    };
    try {
        binaryPersistence.storeBinary(KEY, fileInputStream);
        fail("Must throw BinaryStorageException");
    } catch (final BinaryStorageException e) {
        assertEquals("Test IOException", e.getRootCause().getMessage());
    }

    // (jschumacher) deactivated because it just doesn't fail on linux systems
    // try {
    // binaryPersistence.storeBinary("wrong://3ea9a6a9d68", new byte[] {});
    // fail("Must throw BinaryStorageException");
    // } catch (final BinaryStorageException e) {
    // ; // ok
    // }

    try {
        binaryPersistence.fetchSize("wrong://3ea9a6a9d68");
        fail("Must throw BinaryStorageException");
    } catch (final BinaryStorageException e) {
        ; // ok
    }

    try {
        binaryPersistence.loadBinaryAsInputStream("wrong://3ea9a6a9d68");
        fail("Must throw BinaryStorageException");
    } catch (final BinaryStorageException e) {
        ; // ok
    }
}

From source file:com.adaptris.core.MarshallingBaseCase.java

public void testUncheckedUnmarshalFromInputStream_WithException() throws Exception {
    AdaptrisMarshaller marshaller = createMarshaller();
    Adapter adapter = createMarshallingObject();
    try {/*from w w  w.  j a v  a  2 s  .  c om*/
        Object o = AdaptrisMarshaller.uncheckedUnmarshal(marshaller, adapter, () -> {
            return new InputStream() {
                @Override
                public int read() throws IOException {
                    throw new IOException("testUncheckedUnmarshalFromInputStream_WithException");
                }

                @Override
                public int read(byte[] cbuf, int off, int len) throws IOException {
                    throw new IOException("testUncheckedUnmarshalFromInputStream_WithException");
                }
            };

        });
        fail();
    } catch (RuntimeException expected) {

    }
}

From source file:io.airlift.jaxrs.TestJsonMapper.java

@Test(expectedExceptions = IOException.class)
public void testOtherIOExceptionThrowsIOException() throws IOException {
    try {/*  ww w .  j  a  v  a  2 s.  co m*/
        JsonMapper jsonMapper = new JsonMapper(new ObjectMapper());
        jsonMapper.readFrom(Object.class, Object.class, null, null, null, new InputStream() {
            @Override
            public int read() throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b) throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                throw new ZipException("forced ZipException");
            }
        });
        Assert.fail("Should have thrown an IOException");
    } catch (WebApplicationException e) {
        Assert.fail("Should not have received a WebApplicationException", e);
    }
}

From source file:de.dal33t.powerfolder.test.transfer.BandwidthLimitText.java

public void testHeavyLoad() {
    BandwidthLimiter bl = BandwidthLimiter.LAN_INPUT_BANDWIDTH_LIMITER;
    bl.setAvailable(0);//from w w  w .  j  a  va  2  s.  c o m
    provider.start();
    provider.setLimitBPS(bl, 1024 * 100);
    LimitedInputStream in = new LimitedInputStream(bl, new InputStream() {
        @Override
        public int read() throws IOException {
            return 0;
        }
    });
    Thread pool[] = new Thread[400];
    for (int i = 0; i < pool.length; i++)
        pool[i] = new Thread(new ReaderThread(in, 1000));

    for (int i = 0; i < pool.length; i++)
        pool[i].start();

    try {
        Thread.sleep(6000);
        pool[0].join(1);
    } catch (InterruptedException e) {
        fail(e.toString());
    }

    provider.shutdown();
    assertEquals(Thread.State.TERMINATED, pool[0].getState());
    for (int i = 0; i < pool.length; i++) {
        try {
            pool[i].join(1);
        } catch (InterruptedException e) {
            fail(e.toString());
        }
        assertEquals(Thread.State.TERMINATED, pool[i].getState());
    }
}