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:invar.lib.data.DataParserJson.java

public DataNode parse(InputStream input) throws IOException {
    DataNode root = DataNode.createObject();
    if (input == null || input.available() <= 0) {
        return root;
    }//from  w w w  .  j av a  2s .c  o  m
    JsonParser parser = factory.createParser(input);
    initParser(parser);
    parse(parser, root);
    parser.close();
    return root.numChildren() > 0 ? root.getChild(0) : root;
}

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

private void runningPig() throws InterruptedException, IOException {
    Process ps2 = Runtime.getRuntime()
            .exec("pig -param inputData='/user/admin/SeniorData/linkedmdb-latest-dump.nt' "
                    + "-param outputData='/user/admin/SeniorData/out4' -param reducerNum='12' "
                    + servletContext.getRealPath("/WEB-INF/classes/PigSPARQL_v1.0/test4.pig"));
    ps2.waitFor();/* w  ww. j  a  v a  2  s .  c o 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.epam.wilma.browsermob.transformer.helper.InputStreamConverter.java

/**
 * Converts an inputStream to a String with Apache Commons' IOUtils.
 * @param inputStream InputStream to convert
 * @return converted stream/* ww w .  j  a  va  2  s .com*/
 * @throws ApplicationException when IOUtils or mark/reset fails
 */
public String getStringFromStream(final InputStream inputStream) throws ApplicationException {
    String result = "";
    if (inputStream != null) {
        try {
            int extendedReadLimit = inputStream.available() + BUFFER_SIZE;
            inputStream.mark(extendedReadLimit);
            result = IOUtils.toString(inputStream);
            inputStream.reset();
        } catch (IOException e) {
            throw new ApplicationException("Could not transform request input stream into string!", e);
        }
    }
    return result;
}

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

private void converSparql() throws IOException, InterruptedException {
    System.out.println("converSparql");
    File file = new File(servletContext.getRealPath("/WEB-INF/classes/PigSPARQL_v1.0/"), "test3.pig");
    Process ps = Runtime.getRuntime().exec("java" + " -jar "
            + servletContext.getRealPath("/WEB-INF/classes/PigSPARQL_v1.0/PigSPARQL_main.jar") + "  -e " + "-i "
            + servletContext.getRealPath("/WEB-INF/classes/PigSPARQL_v1.0/test1.sparql") + " -o "
            + servletContext.getRealPath("/WEB-INF/classes/PigSPARQL_v1.0/test3.pig") + " -opt");
    // Then retreive the process output
    //     InputStream in = proc.getInputStream();
    //   InputStream err = proc.getErrorStream();
    ps.waitFor();/*from  w ww . ja v  a  2 s.  c o  m*/
    java.io.InputStream is = ps.getInputStream();
    byte b[] = new byte[is.available()];
    is.read(b, 0, b.length);
    System.out.println(new String(b));
}

From source file:com.wabacus.util.WabacusClassLoader.java

public synchronized Class loadClassFromClassPath(String className) {

    Iterator<String> dirs = classRepository.iterator();
    byte[] classBytes = null;

    while (dirs.hasNext()) {
        String dir = (String) dirs.next();

        String classFileName = className.replace('.', File.separatorChar);
        classFileName += ".class";
        try {/*from  w  w w  . j  a  va2 s  .  co  m*/
            File file = new File(dir + File.separatorChar + classFileName);
            if (file.exists()) {
                InputStream is = new FileInputStream(file);
                classBytes = new byte[is.available()];
                is.read(classBytes);
                break;
            }
        } catch (IOException ex) {

            ex.printStackTrace();
            return null;
        }
    }
    return this.defineClass(className, classBytes, 0, classBytes.length);
}

From source file:io.dacopancm.jfee.managedController.ReportBean.java

public JasperPrint init(String reportName, Collection<?> data) throws JRException {
    try {/*ww  w  .  j  av a  2  s.  co  m*/
        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(data);
        //String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports/report.jasper");
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("report/" + reportName);
        log.info("jfee:" + stream.available());
        return JasperFillManager.fillReport(stream, new HashMap(), beanCollectionDataSource);
    } catch (IOException ex) {
        log.error("jfee: " + ex, ex);
    }
    return null;
}

From source file:klawisch.jimdo.statistics.statisticsdemo.DownloaderThread.java

/**
 * Downloads the content of a specific url and parses this to a JSONObject
 *
 * @param url The url to download the content
 * @return Returns the parsed JSONObject for further processing
 *//*w w  w .  ja  v a  2  s.c  o m*/
private JSONObject getURLContent(String url) {
    if (url != null) {
        try {
            InputStream in = context.getAssets().open(url);

            int size = in.available();
            byte[] buffer = new byte[size];
            in.read(buffer);
            in.close();
            return new JSONObject(new String(buffer, "UTF-8"));

        } catch (Exception e) {
            Log.e(TAG, "WARNING: File \"" + url + "\" not found!");

        }
    }
    return null;
}

From source file:org.openmrs.contrib.metadatarepository.webapp.controller.FileUploadControllerTest.java

@Test
public void testOnSubmit() throws Exception {

    request = newPost("/packageupload.html");
    request.setRemoteUser("user");

    pkg.setName("Surgery");
    pkg.setDescription("Hospital Albert Schweitzer Haiti Surgery Form");
    pkg.setUser(umagr.getUserByUsername("user"));
    pkg.setVersion(1L);/*from   w  ww . j  a va 2  s .  c  o  m*/
    pkg.setOpenmrsVersion("1.8.3  Build 24510");
    pkg.setDateCreated(new Date());
    pkg.setGroupUuid("6ca304bf-9b70-4063-a669-c57d710d55aa");
    pkg.setSubscriptionUrl("");
    pkg.setDownloadCount(0L);
    InputStream fis = getClass().getResourceAsStream("/Surgery.zip");
    if (fis != null) {
        ByteArrayOutputStream data = new ByteArrayOutputStream(fis.available());
        IOUtils.copy(fis, data);
        pkg.setFile(data.toByteArray());
    }
    request.addParameter("upload", "");

    BindingResult errors = new DataBinder(pkg).getBindingResult();
    String test = f.onSubmit(pkg, errors, request);

    assertNotNull(test);

}

From source file:in.co.sneh.controller.CargaExcelRural.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  w  ww. j a v a2 s. c  o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        CargaExcelReqRural lee = new CargaExcelReqRural();
        String Unidad = "";

        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
        if (isMultiPart) {
            ServletFileUpload upload = new ServletFileUpload();
            try {
                HttpSession sesion = request.getSession(true);
                FileItemIterator itr = upload.getItemIterator(request);
                while (itr.hasNext()) {
                    FileItemStream item = itr.next();
                    if (item.isFormField()) {
                        String fielName = item.getFieldName();
                        InputStream is = item.openStream();
                        byte[] b = new byte[is.available()];
                        is.read(b);
                        String value = new String(b);
                        response.getWriter().println(fielName + ":" + value + "<br/>");
                    } else {
                        String path = getServletContext().getRealPath("/");
                        if (CargaExcelRuralModel.processFile(path, item)) {
                            //response.getWriter().println("file uploaded successfully");
                            if (lee.obtieneArchivo(path, item.getName())) {
                                out.println("<script>alert('Se carg el Folio Correctamente')</script>");
                                out.println(
                                        "<script>window.location='facturacionRural/cargaRequerimento.jsp'</script>");
                            }
                            //response.sendRedirect("cargaFotosCensos.jsp");
                        } else {
                            //response.getWriter().println("file uploading falied");
                            //response.sendRedirect("cargaFotosCensos.jsp");
                        }
                    }
                }
            } catch (FileUploadException fue) {
                fue.printStackTrace();
            }
            out.println("<script>alert('No se pudo cargar el Folio, verifique las celdas')</script>");
            out.println("<script>window.location='requerimiento.jsp'</script>");
            //response.sendRedirect("carga.jsp");
        }
    } finally {
        out.close();
    }
}

From source file:flexus.web.servlet.StartMenusServlet.java

/**
 * @param request/*w w  w  . j ava 2 s  . c o m*/
 * @param response
 */
private void doProcess(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding(charsetName);
    response.setCharacterEncoding(charsetName);

    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("startMenus.xml");
    byte[] bytes = null;
    if (stream != null && stream.available() > 0) {
        bytes = IOUtils.toByteArray(stream);
    }

    response.setContentType("text/xml");

    if (bytes != null) {
        response.getOutputStream().write(bytes);
        response.getOutputStream().flush();
    }
}