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:ir.rasen.charsoo.controller.image_loader.core.download.BaseImageDownloader.java

/**
 * Retrieves {@link InputStream} of image by URI (image is located in the network).
 *
 * @param imageUri Image URI//from  w  w w.j  a  v  a 2s. c o m
 * @param extra    Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@link InputStream} of image
 * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
 *                     URL.
 */
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException, JSONException {
    HttpURLConnection conn = createConnection(imageUri, extra);
    String strImage = "";

    int redirectCount = 0;
    while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
        conn = createConnection(conn.getHeaderField("Location"), extra);
        redirectCount++;
    }

    InputStream imageStream = new InputStream() {
        @Override
        public int read() throws IOException {
            return 0;
        }
    };
    try {
        imageStream = conn.getInputStream();
        StringBuilder sb = new StringBuilder();
        String line;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(imageStream));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        JSONObject jsonObject = new JSONObject(sb.toString());
        if (jsonObject != null) {
            strImage = new JSONObject(jsonObject.getString(Params.RESULT)).getString(Params.IMAGE);
            imageStream = new ByteArrayInputStream(strImage.getBytes());
        } else {
            imageStream = conn.getInputStream();
        }
    } catch (Exception e) {
        // Read all data to allow reuse connection (http://bit.ly/1ad35PY)
        IoUtils.readAndCloseStream(conn.getErrorStream());
        try {
            throw e;
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }
    if (!shouldBeProcessed(conn)) {
        IoUtils.closeSilently(imageStream);
        throw new IOException("Image request failed with response code " + conn.getResponseCode());
    }
    return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), strImage.length());
}

From source file:org.commoncrawl.hadoop.io.deprecated.ArcFileReader.java

/**
 * Costructs a new ArcFileReader object with specified block size (for
 * allocations)/*from   w  w w  .  j a va2 s  .co m*/
 */
public ArcFileReader() {

    super(_dummyStream, new Inflater(true), _blockSize);
    // set up buffer queue ...
    _consumerQueue = new LinkedBlockingQueue<BufferItem>(_bufferQueueSize);
    // setup the proper stream...
    super.in = new PushbackInputStream(new InputStream() {

        ByteBuffer _activeBuffer = null;
        byte oneByteArray[] = new byte[1];

        @Override
        public int read() throws IOException {
            if (read(oneByteArray, 0, 1) != -1) {
                return oneByteArray[0] & 0xff;
            }
            return -1;
        }

        @Override
        public int read(byte b[], int off, int len) throws IOException {
            if (_activeBuffer == null || _activeBuffer.remaining() == 0) {
                BufferItem nextItem = null;
                try {
                    // when io timeout is not specified, block indefinitely...
                    if (_ioTimeoutValue == -1) {
                        nextItem = _consumerQueue.take();
                    }
                    // otherwise wait for specified time on io
                    else {
                        nextItem = _consumerQueue.poll(_ioTimeoutValue, TimeUnit.MILLISECONDS);

                        if (nextItem == null) {
                            throw new IOException("IO Timeout waiting for Buffer");
                        }
                    }

                } catch (InterruptedException e) {
                    throw new IOException("Thread Interrupted waiting for Buffer");
                }

                if (nextItem._buffer == null) {
                    _eosReached = true;
                    // EOF CONDITION ...
                    return -1;
                } else {
                    _activeBuffer = nextItem._buffer;
                }
            }
            final int sizeAvailable = _activeBuffer.remaining();
            final int readSize = Math.min(sizeAvailable, len);

            _activeBuffer.get(b, off, readSize);

            _streamPos += readSize;

            return readSize;
        }
    }, _blockSize);
}

From source file:org.jcodec.common.io.Buffer.java

public InputStream is() {
    return new InputStream() {
        public int read() throws IOException {
            return next();
        }//from   www .j  a  va 2 s. com
    };
}

From source file:com.aerofs.baseline.http.TestHttpRequestHandler.java

@Test
public void shouldSuccessfullyPostAndReceiveResponseAfterMakingUnsuccessfulPost() throws Exception {
    // unsuccessful
    // how does this test work, you ask?
    // well, there's only one request processing
    // thread on the server so if that thread locks up waiting
    // for bytes that never come, even if the stream is closed
    // then the *second* request will time out.
    try {/*www . j  av  a2 s. co m*/
        HttpPost post0 = new HttpPost(
                ServiceConfiguration.SERVICE_URL + "/" + Resources.BASIC_RESOURCE + "/data1");
        post0.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
        BasicHttpEntity basic = new BasicHttpEntity();
        basic.setChunked(true);
        basic.setContentLength(-1);
        basic.setContent(new InputStream() {

            private int counter = 0;

            @Override
            public int read() throws IOException {
                if (counter < (3 * 1024 * 1024)) {
                    counter++;
                    return 'a';
                } else {
                    throw new IOException("read failed");
                }
            }
        });
        post0.setEntity(basic);

        Future<HttpResponse> future0 = client.getClient().execute(post0, null);
        future0.get();
    } catch (Exception e) {
        // noop
    }

    // successful
    HttpPost post = new HttpPost(ServiceConfiguration.SERVICE_URL + "/" + Resources.BASIC_RESOURCE + "/data1");
    post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
    post.setEntity(HttpUtils.writeStringToEntity("data2"));

    Future<HttpResponse> future = client.getClient().execute(post, null);
    HttpResponse response = future.get(10, TimeUnit.SECONDS);

    assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_OK));
    assertThat(HttpUtils.readStreamToString(response.getEntity().getContent()), equalTo("data1-data2"));
}

From source file:org.commoncrawl.util.StreamingArcFileReader.java

/** 
 * Costructs a new StreamingArcFileReader object
 * /*from   w w  w. j ava 2  s  .  co m*/
 */

public StreamingArcFileReader(boolean hasArcFileHeader) {

    // setup the proper stream...
    _rawInput = new InputStream() {

        byte oneByteArray[] = new byte[1];

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

        @Override
        public int read() throws IOException {
            if (read(oneByteArray, 0, 1) != -1) {
                _streamPos++;
                return oneByteArray[0] & 0xff;
            }
            return -1;
        }

        @Override
        public int read(byte b[], int off, int len) throws IOException {
            if (_activeInputBuffer == null || _activeInputBuffer.remaining() == 0) {

                _activeInputBuffer = null;

                BufferItem nextItem = null;

                try {
                    if (_consumerQueue.size() != 0) {
                        nextItem = _consumerQueue.take();
                    }
                } catch (InterruptedException e) {
                }

                if (nextItem != null) {
                    if (nextItem._buffer == null) {
                        return -1;
                    } else {
                        _activeInputBuffer = nextItem._buffer;
                    }
                }
            }

            if (_activeInputBuffer != null || _activeInputBuffer.remaining() != 0) {

                final int sizeAvailable = _activeInputBuffer.remaining();
                final int sizeToRead = Math.min(sizeAvailable, len);

                _activeInputBuffer.get(b, off, sizeToRead);

                _streamPos += sizeToRead;

                synchronized (this) {
                    _bytesAvailable -= sizeToRead;
                }

                return sizeToRead;
            } else {
                return 0;
            }
        }
    };

    _checkedInput = new CheckedInputStream(_rawInput, _crc);
    if (!hasArcFileHeader) {
        _readState = ReadState.ReadingEntryHeader;
    }
}

From source file:com.hortonworks.registries.storage.filestorage.DbFileStorageTest.java

@Test(expected = StorageException.class)
public void testConcurrentUpload() throws Throwable {
    try {//  w  w  w  .  j a  v  a 2s.  c om
        transactionManager.beginTransaction(TransactionIsolation.SERIALIZABLE);
        String input = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(FILE_NAME),
                "UTF-8");
        String updated = input + " new text";
        dbFileStorage.upload(IOUtils.toInputStream(input, "UTF-8"), FILE_NAME);
        InputStream slowStream = new InputStream() {
            byte[] bytes = updated.getBytes("UTF-8");
            int i = 0;

            @Override
            public int read() throws IOException {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                }
                return (i < bytes.length) ? (bytes[i++] & 0xff) : -1;
            }
        };
        FutureTask<String> ft1 = new FutureTask<>(() -> {
            try {
                transactionManager.beginTransaction(TransactionIsolation.SERIALIZABLE);
                String name = dbFileStorage.upload(slowStream, FILE_NAME);
                transactionManager.commitTransaction();
                return name;
            } catch (Exception e) {
                transactionManager.rollbackTransaction();
                throw e;
            }
        });
        FutureTask<String> ft2 = new FutureTask<>(() -> {
            try {
                transactionManager.beginTransaction(TransactionIsolation.SERIALIZABLE);
                String name = dbFileStorage.upload(IOUtils.toInputStream(updated, "UTF-8"), FILE_NAME);
                transactionManager.commitTransaction();
                return name;
            } catch (Exception e) {
                transactionManager.rollbackTransaction();
                throw e;
            }
        });
        Thread t1 = new Thread(ft1);
        Thread t2 = new Thread(ft2);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        try {
            ft1.get();
        } catch (ExecutionException ex) {
            throw ex.getCause();
        }
        transactionManager.commitTransaction();
    } catch (Exception e) {
        transactionManager.rollbackTransaction();
        throw e;
    }
}

From source file:org.jboss.pnc.auth.keycloakutil.util.HttpUtil.java

public static HeadersBodyStatus doRequest(String type, String url, HeadersBody request) throws IOException {
    HttpRequestBase req;/*from www.j a v  a2  s.  c o m*/
    switch (type) {
    case "get":
        req = new HttpGet(url);
        break;
    case "post":
        req = new HttpPost(url);
        break;
    case "put":
        req = new HttpPut(url);
        break;
    case "delete":
        req = new HttpDelete(url);
        break;
    case "options":
        req = new HttpOptions(url);
        break;
    case "head":
        req = new HttpHead(url);
        break;
    default:
        throw new RuntimeException("Method not supported: " + type);
    }
    addHeaders(req, request.getHeaders());

    if (request.getBody() != null) {
        if (req instanceof HttpEntityEnclosingRequestBase == false) {
            throw new RuntimeException("Request type does not support body: " + type);
        }
        ((HttpEntityEnclosingRequestBase) req).setEntity(new InputStreamEntity(request.getBody()));
    }

    HttpResponse res = getHttpClient().execute(req);
    InputStream responseStream = null;
    if (res.getEntity() != null) {
        responseStream = res.getEntity().getContent();
    } else {
        responseStream = new InputStream() {
            @Override
            public int read() throws IOException {
                return -1;
            }
        };
    }

    Headers headers = new Headers();
    HeaderIterator it = res.headerIterator();
    while (it.hasNext()) {
        org.apache.http.Header header = it.nextHeader();
        headers.add(header.getName(), header.getValue());
    }

    return new HeadersBodyStatus(res.getStatusLine().toString(), headers, responseStream);
}

From source file:org.codehaus.mojo.keytool.GenkeyMojo.java

public void execute() throws MojoExecutionException {

    List arguments = new ArrayList();

    Commandline commandLine = new Commandline();

    commandLine.setExecutable(getKeytoolPath());

    arguments.add("-genkey");

    addArgIf(arguments, this.verbose, "-v");

    // I believe Commandline to add quotes where appropriate, although I haven't tested it enough.
    addArgIfNotEmpty(arguments, "-dname", this.dname);
    addArgIfNotEmpty(arguments, "-alias", this.alias);
    addArgIfNotEmpty(arguments, "-keypass", this.keypass);
    addArgIfNotEmpty(arguments, "-keystore", this.keystore);
    addArgIfNotEmpty(arguments, "-storepass", this.storepass);
    addArgIfNotEmpty(arguments, "-validity", this.validity);
    addArgIfNotEmpty(arguments, "-keyalg", this.keyalg);
    addArgIfNotEmpty(arguments, "-keysize", this.keysize);
    addArgIfNotEmpty(arguments, "-sigalg", this.sigalg);
    addArgIfNotEmpty(arguments, "-storetype", this.storetype);

    for (Iterator it = arguments.iterator(); it.hasNext();) {
        commandLine.createArgument().setValue(it.next().toString());
    }/*from  ww  w.  j a v  a  2 s  .  co  m*/

    if (workingDirectory != null) {
        commandLine.setWorkingDirectory(workingDirectory.getAbsolutePath());
    }

    createParentDirIfNecessary(keystore);

    getLog().debug("Executing: " + commandLine);

    // jarsigner may ask for some input if the parameters are missing or incorrect.
    // This should take care of it and make it fail gracefully
    final InputStream inputStream = new InputStream() {
        public int read() {
            return -1;
        }
    };
    StreamConsumer outConsumer = new StreamConsumer() {
        public void consumeLine(String line) {
            getLog().info(line);
        }
    };

    final StringBuffer errBuffer = new StringBuffer();
    StreamConsumer errConsumer = new StreamConsumer() {
        public void consumeLine(String line) {
            getLog().warn(line);
            errBuffer.append(line);
        }
    };

    try {
        int result = executeCommandLine(commandLine, inputStream, outConsumer, errConsumer);

        if (result != 0) {
            throw new MojoExecutionException("Result of " + commandLine + " execution is: \'" + result + "\': "
                    + errBuffer.toString() + ".");
        }
    } catch (CommandLineException e) {
        throw new MojoExecutionException("command execution failed", e);
    }
}

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

/**
 * test list/delete objects more than 1000
 *
 * @throws Exception//  ww w  .j a v a 2s.  co  m
 */
public void testObjectPagination() throws Exception {

    final InputStream nullStream = new InputStream() {
        @Override
        public int read() throws IOException {
            return -1;
        }
    };
    client.putObject("hadoop-intg", "test/", nullStream);
    for (int i = 0; i < 1010; i++) {
        System.out.println("putObject:" + "test/" + i);
        client.putObject("hadoop-intg", "test/" + i, nullStream);
    }
    assertEquals(1010, fileSystem.listStatus(path("/test")).length);
    fileSystem.delete(path("/test"), true);

}

From source file:edu.umn.msi.tropix.common.io.IOUtilsTest.java

@Test(groups = "unit", expectedExceptions = IORuntimeException.class)
public void toStringStreamError() {
    this.ioUtils.toString(new InputStream() {
        public int read() throws IOException {
            throw new IOException();
        }//from w  w  w  .  ja va  2 s .com
    });
}