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:org.sonar.fortify.fvdl.FortifyReportFile.java

private InputStream getInputStreamFromFprFile(File file) throws IOException {
    final ZipFile fprFile = new ZipFile(file);
    try {//w  ww .  j a  va2  s .c o  m
        final InputStream reportStream = fprFile
                .getInputStream(fprFile.getEntry(FortifyConstants.AUDIT_FVDL_FILE));
        return new InputStream() {
            @Override
            public int read() throws IOException {
                return reportStream.read();
            }

            @Override
            public void close() throws IOException {
                try {
                    reportStream.close();
                } finally {
                    fprFile.close();
                }
            }
        };
    } catch (IOException e) {
        fprFile.close();
        throw e;
    }
}

From source file:com.googlecode.xmlzen.utils.FileUtilsTest.java

@Test
public void testClose() throws Exception {
    FileUtils.close(null);//from   w ww .  j a  v a2 s. com
    FileInputStream in = new FileInputStream(FileUtils.getClassPathFile("xmls/note.xml"));
    in.read();
    FileUtils.close(in);
    try {
        in.read();
        fail("Stream should be closed: " + in);
    } catch (final IOException e) {
        //expected
    }
    FileUtils.close(in);
    try {
        FileUtils.close(new InputStream() {
            @Override
            public int read() throws IOException {
                return 0;
            }

            @Override
            public void close() throws IOException {
                throw new IOException("Fake close exception");
            }
        });
    } catch (final RuntimeException e) {
        fail("Should be caught somewhere else");
    }
}

From source file:org.efaps.webdav4vfs.test.ramvfs.RamFileRandomAccessContent.java

/**
 * @param _file     file to access//  ww w  .j  a  va2 s . c om
 */
public RamFileRandomAccessContent(final RamFileObject _file) {
    super();
    this.buf = _file.getData().getBuffer();
    this.file = _file;

    this.rafis = new InputStream() {
        @Override()
        public int read() throws IOException {
            int ret;
            try {
                ret = readByte();
            } catch (final EOFException e) {
                ret = -1;
            }
            return ret;
        }

        @Override()
        public long skip(final long _n) throws IOException {
            seek(getFilePointer() + _n);
            return _n;
        }

        @Override()
        public void close() {
        }

        @Override()
        public int read(final byte[] _b) throws IOException {
            return read(_b, 0, _b.length);
        }

        @Override()
        public int read(final byte[] _b, final int _off, final int _len) throws IOException {
            final int retLen = Math.min(_len, getLeftBytes());
            RamFileRandomAccessContent.this.readFully(_b, _off, retLen);
            return retLen;
        }

        @Override()
        public int available() throws IOException {
            return getLeftBytes();
        }
    };
}

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

@Test
public void testEOFExceptionReturnsJsonMapperParsingException() throws IOException {
    try {/*from w w  w  . j a v  a  2  s  .  co  m*/
        mapper.readFrom(Object.class, Object.class, null, null, null, new InputStream() {
            @Override
            public int read() throws IOException {
                throw new EOFException("forced EOF Exception");
            }

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

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                throw new EOFException("forced EOF Exception");
            }
        });
        fail("Should have thrown a JsonMapperParsingException");
    } catch (JsonMapperParsingException e) {
        Assert.assertTrue((e.getMessage()).startsWith("Invalid json for Java type"));
    }
}

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

@Test
public void testEOFExceptionReturnsJsonMapperParsingException() throws IOException {
    try {/*from  w ww  .  j av a2 s . c o  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 EOFException("forced EOF Exception");
            }

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

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                throw new EOFException("forced EOF Exception");
            }
        });
        Assert.fail("Should have thrown a JsonMapperParsingException");
    } catch (JsonMapperParsingException e) {
        Assert.assertTrue((e.getMessage()).startsWith("Invalid json for Java type"));
    }
}

From source file:com.ettrema.restlet.RequestAdapter.java

@Override
public InputStream getInputStream() throws IOException {
    // Milton doesn't check for null InputStream, but there are some places where it checks for -1
    // Don't call target.isEntityAvailable(), doesn't work for PUT, see its source...
    if (getTarget().getEntity() != null && getTarget().getEntity().isAvailable()) {
        InputStream stream = getTarget().getEntity().getStream();
        if (stream != null)
            return stream;
    }//from w  w w  .ja  va  2 s.  c o  m
    return new InputStream() {
        @Override
        public int read() throws IOException {
            return -1;
        }
    };
}

From source file:com.ksc.http.timers.client.AbortedExceptionClientExecutionTimerTest.java

@Ignore
@Test(timeout = TEST_TIMEOUT, expected = ClientExecutionTimeoutException.class)
public void clientExecutionTimeoutEnabled_aborted_exception_occurs_timeout_expired() throws Exception {
    ClientConfiguration config = new ClientConfiguration().withClientExecutionTimeout(CLIENT_EXECUTION_TIMEOUT)
            .withMaxErrorRetry(0);//from ww w .j av  a 2  s  .c  om
    ConnectionManagerAwareHttpClient rawHttpClient = createRawHttpClientSpy(config);

    httpClient = new KSCHttpClient(config, rawHttpClient, null);

    execute(httpClient, new EmptyHttpRequest(server.getEndpoint(), HttpMethodName.PUT,
            new SdkBufferedInputStream(new InputStream() {
                @Override
                public int read() throws IOException {
                    return 1;
                }
            })));
}

From source file:hudson.model.LargeText.java

/**
 * Returns {@link Reader} for reading the raw bytes.
 *//*from  w w w .  jav  a  2 s .co m*/
public Reader readAll() throws IOException {
    return new InputStreamReader(new InputStream() {
        final Session session = source.open();

        public int read() throws IOException {
            byte[] buf = new byte[1];
            int n = session.read(buf);
            if (n == 1)
                return buf[0];
            else
                return -1; // EOF
        }

        public int read(byte[] buf, int off, int len) throws IOException {
            return session.read(buf, off, len);
        }

        public void close() throws IOException {
            session.close();
        }
    });
}

From source file:org.apache.olingo.server.core.debug.DebugTabBodyTest.java

@Test
public void streamError() throws Exception {
    ODataResponse response = new ODataResponse();
    InputStream input = new InputStream() {
        @Override//from  www  .  ja  va 2s  . c  om
        public int read() throws IOException {
            throw new IOException("test");
        }
    };
    response.setContent(input);
    assertEquals("\"Could not parse Body for Debug Output\"", createJson(new DebugTabBody(response)));
    assertEquals("<pre class=\"code\">\nCould not parse Body for Debug Output\n</pre>\n",
            createHtml(new DebugTabBody(response)));
}

From source file:es.uvigo.ei.sing.jarvest.core.HTTPUtils.java

public static InputStream getURLBody(final String url, StringBuffer charset,
        Map<String, String> additionalHeaders) throws IOException {
    HttpClient client = getClient();//from   ww  w  . ja  va2 s . c o  m
    System.err.println("Connecting to: " + url);
    final GetMethod get = new GetMethod(url);

    addHeaders(additionalHeaders, get);

    try {
        client.executeMethod(get);
        charset.append(get.getResponseCharSet());

        final InputStream in = get.getResponseBodyAsStream();
        return new InputStream() {

            @Override
            public int read() throws IOException {
                return in.read();
            }

            @Override
            public void close() {
                get.releaseConnection();
            }

        };
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}