Example usage for java.io InputStream available

List of usage examples for java.io InputStream available

Introduction

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

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

Usage

From source file:fr.mixit.android.io.JsonExecutor.java

public <T> T executeAndGet(Context context, String assetName, JsonHandlerGet<T> handler)
        throws JsonHandlerException {
    try {/*from   w ww  . j  av  a 2 s. c  o m*/
        final InputStream input = context.getAssets().open(assetName);
        final byte[] buffer = new byte[input.available()];
        while (input.read(buffer) != -1) {
            ;
        }
        final String jsontext = new String(buffer);
        return executeAndGet(jsontext, handler);
    } catch (final JsonHandlerException e) {
        throw e;
    } catch (final IOException e) {
        throw new JsonHandlerException("Problem parsing local asset: " + assetName, e);
    }
}

From source file:com.ejisto.modules.dao.remote.BaseRemoteDao.java

private byte[] readInputStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(in.available());
    int read;/*  w w w  .  j a v a2s.  c om*/
    byte[] buffer = new byte[4096];
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    return out.toByteArray();
}

From source file:DBMS.PicUpdateServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from w  w  w .  j  ava2s.com*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
    if (isMultiPart) {
        ServletFileUpload upload = new ServletFileUpload();
        try {
            FileItemIterator itr = upload.getItemIterator(request);
            while (itr.hasNext()) {
                FileItemStream item = itr.next();
                if (item.isFormField()) {
                    String fieldName = item.getFieldName();
                    InputStream is = item.openStream();
                    byte[] b = new byte[is.available()];
                    is.read(b);
                    String value = new String(b);
                    System.out.println("Getting");
                } else {
                    String path = getServletContext().getRealPath("/");
                    loginid lid = null;
                    HttpSession session = request.getSession();
                    lid = (loginid) session.getAttribute("lid");
                    int id = lid.getId();
                    if (UpdateFileUpload.processFile(path, item, id)) {
                        response.sendRedirect("ProfilePicUpdateSuccess.jsp");
                    } else
                        response.sendRedirect("ProfilePicUpdateFailed.jsp");
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.si.services.camel.thumbnailator.SpringThumbnailatorComponentTest.java

@Test
public void testSpringRoute() throws InterruptedException, IOException {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);/*w  w  w.  j  a va2  s .co  m*/
    String name = "HappyBall.jpg";
    InputStream input = this.getClass().getResourceAsStream("/" + name);

    template.sendBody("direct:start", input);

    assertMockEndpointsSatisfied();

    InputStream output = this.getClass().getResourceAsStream(String.format("/Thumbnail-%s", name));
    byte[] expected = new byte[output.available()];
    int read = output.read(expected);

    byte[] actual = mock.getExchanges().get(0).getIn().getBody(byte[].class);

    Assert.assertEquals("Didn't read all the expected bytes", expected.length, read);
    Assert.assertEquals("Size is the same", expected.length, actual.length);
}

From source file:com.antonjohansson.svncommit.core.utils.Bash.java

private boolean isAvailable(Process process, InputStream logStream, InputStream errorStream)
        throws IOException {
    return logStream.available() > 0 || errorStream.available() > 0 || process.isAlive();
}

From source file:org.bcsphere.components.BCWebViewClient.java

private String readFileContent(String path) {
    String result = "";
    try {//  w ww  .j  a  va2 s  .c o  m
        InputStream in = this.mMainActivity.getResources().getAssets().open(path);
        int length = in.available();
        byte[] buffer = new byte[length];
        in.read(buffer);
        result = EncodingUtils.getString(buffer, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:net.sf.taverna.t2.reference.RegisterLargeByteArrayTest.java

private byte[] readBinaryData(String resourceName) throws Exception {
    InputStream instr = RegisterLargeByteArrayTest.class.getResourceAsStream("/data/" + resourceName);
    int size = instr.available();
    byte[] result = new byte[size];
    instr.read(result);//from   ww  w. j  a va2 s . c o  m
    return result;
}

From source file:br.com.ifpb.bdnc.projeto.geo.servlets.CadastraImagem.java

private Image mountImage(HttpServletRequest request) {
    Image image = new Image();
    image.setCoord(new Coordenate());
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        ServletFileUpload upload = new ServletFileUpload();
        try {/*w w  w  .  j a v a 2 s. co m*/
            FileItemIterator itr = upload.getItemIterator(request);
            while (itr.hasNext()) {
                FileItemStream item = itr.next();
                if (item.isFormField()) {
                    InputStream in = item.openStream();
                    byte[] b = new byte[in.available()];
                    in.read(b);
                    if (item.getFieldName().equals("description")) {
                        image.setDescription(new String(b));
                    } else if (item.getFieldName().equals("authors")) {
                        image.setAuthors(new String(b));
                    } else if (item.getFieldName().equals("end")) {
                        image.setDate(
                                LocalDate.parse(new String(b), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                    } else if (item.getFieldName().equals("latitude")) {
                        image.getCoord().setLat(new String(b));
                    } else if (item.getFieldName().equals("longitude")) {
                        image.getCoord().setLng(new String(b));
                    } else if (item.getFieldName().equals("heading")) {
                        image.getCoord().setHeading(new String(b));
                    } else if (item.getFieldName().equals("pitch")) {
                        image.getCoord().setPitch(new String(b));
                    } else if (item.getFieldName().equals("zoom")) {
                        image.getCoord().setZoom(new String(b));
                    }
                } else {
                    if (!item.getName().equals("")) {
                        MultipartData md = new MultipartData();
                        String folder = "historicImages";
                        md.setFolder(folder);
                        String path = request.getServletContext().getRealPath("/");
                        System.out.println(path);
                        String nameToSave = "pubImage" + Calendar.getInstance().getTimeInMillis()
                                + item.getName();
                        image.setImagePath(folder + "/" + nameToSave);
                        md.saveImage(path, item, nameToSave);
                        String imageMinPath = folder + "/" + "min" + nameToSave;
                        RedimencionadorImagem.resize(path, folder + "/" + nameToSave,
                                path + "/" + imageMinPath.toString(), IMAGE_MIN_WIDTH, IMAGE_MIM_HEIGHT);
                        image.setMinImagePath(imageMinPath);
                    }
                }
            }
        } catch (FileUploadException | IOException ex) {
            System.out.println("Erro ao manipular dados");
        }
    }
    return image;
}

From source file:com.seniorproject.semanticweb.services.HadoopService.java

private void deleteFolderFromHadoop() throws IOException, InterruptedException {
    Process ps2 = Runtime.getRuntime().exec("hadoop fs -rm -r /user/admin/SeniorData/out4");
    ps2.waitFor();/*from   www .  j  av  a 2s.co m*/
    java.io.InputStream is2 = ps2.getInputStream();
    byte b2[] = new byte[is2.available()];
    is2.read(b2, 0, b2.length);
    System.out.println(new String(b2));
}

From source file:com.seniorproject.semanticweb.services.HadoopService.java

private void mergeHadoopFile() throws IOException, InterruptedException {
    File file = new File(servletContext.getRealPath("/WEB-INF/classes/PigSPARQL_v1.0/"), "output.txt");
    Process ps = Runtime.getRuntime().exec("hadoop fs -getmerge /user/admin/SeniorData/out4 "
            + servletContext.getRealPath("/WEB-INF/classes/PigSPARQL_v1.0/output.txt"));
    // Then retreive the process output
    //     InputStream in = proc.getInputStream();
    //   InputStream err = proc.getErrorStream();
    ps.waitFor();//from w  w w  . jav a2  s  .c  om
    java.io.InputStream is = ps.getInputStream();
    byte b[] = new byte[is.available()];
    is.read(b, 0, b.length);
    System.out.println(new String(b));
}