Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:org.kie.guvnor.m2repo.backend.server.GuvnorM2Repository.java

public static String loadPOMFromJar(InputStream jarInputStream) {
    try {//from w  ww .  ja va  2 s .co  m
        ZipInputStream zis = new ZipInputStream(jarInputStream);
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {
            //System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
            // consume all the data from this entry
            if (entry.getName().startsWith("META-INF/maven") && entry.getName().endsWith("pom.xml")) {
                InputStreamReader isr = new InputStreamReader(zis, "UTF-8");
                StringBuilder sb = new StringBuilder();
                for (int c = isr.read(); c != -1; c = isr.read()) {
                    sb.append((char) c);
                }
                return sb.toString();
            }
        }
    } catch (ZipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:org.jboss.additional.testsuite.jdkall.present.domain.management.cli.DomainDeploymentOverlayTestCase.java

public static String getContent(HttpResponse response) throws IOException {
    InputStreamReader reader = new InputStreamReader(response.getEntity().getContent());
    StringBuilder content = new StringBuilder();
    int c;//from  www. j  a va 2s  .  c om
    while (-1 != (c = reader.read())) {
        content.append((char) c);
    }
    reader.close();
    return content.toString();
}

From source file:org.pentaho.di.core.vfs.KettleVFS.java

public static String getTextFileContent(String vfsFilename, VariableSpace space, String charSetName)
        throws KettleFileException {
    try {/*from  ww w.j a v  a2s  . co m*/
        InputStream inputStream = null;

        if (space == null) {
            inputStream = getInputStream(vfsFilename);
        } else {
            inputStream = getInputStream(vfsFilename, space);
        }
        InputStreamReader reader = new InputStreamReader(inputStream, charSetName);
        int c;
        StringBuffer stringBuffer = new StringBuffer();
        while ((c = reader.read()) != -1) {
            stringBuffer.append((char) c);
        }
        reader.close();
        inputStream.close();

        return stringBuffer.toString();
    } catch (IOException e) {
        throw new KettleFileException(e);
    }
}

From source file:eu.project.ttc.tools.cli.TermSuiteCLIUtils.java

/**
 * Read the standard input as a string./*from  ww  w . j  a  v a 2  s .co m*/
 * @return
 * @throws IOException
 */
public static String readIn(String encoding) throws IOException {
    if (in == null) {
        InputStreamReader reader = new InputStreamReader(System.in, encoding);
        List<Character> chars = new LinkedList<Character>();
        int c;
        while (reader.ready() && ((c = reader.read()) != -1))
            chars.add((char) c);

        char[] charAry = new char[chars.size()];
        for (int i = 0; i < chars.size(); i++)
            charAry[i] = chars.get(i);
        in = new String(charAry);
    }
    return in;
}

From source file:org.wso2.carbon.integration.common.utils.FileManager.java

public static void copyFile(File sourceFile, String destinationPath) throws IOException {
    File destinationFile = new File(destinationPath);
    InputStreamReader in = null;
    OutputStreamWriter out = null;
    int c;/*  www .j  a  va 2s.c  o m*/
    try {
        in = new InputStreamReader(new FileInputStream(sourceFile), StandardCharsets.UTF_8);
        out = new OutputStreamWriter(new FileOutputStream(destinationFile), StandardCharsets.UTF_8);
        while ((c = in.read()) != -1) {
            out.write(c);
        }
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            //ignore
        }
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:pt.webdetails.cns.notifications.sparkl.kettle.baserver.web.utils.HttpConnectionHelper.java

public static Response callHttp(String url, String user, String password)
        throws IOException, KettleStepException {

    // used for calculating the responseTime
    long startTime = System.currentTimeMillis();

    HttpClient httpclient = SlaveConnectionManager.getInstance().createHttpClient();
    HttpMethod method = new GetMethod(url);
    httpclient.getParams().setAuthenticationPreemptive(true);
    Credentials credentials = new UsernamePasswordCredentials(user, password);
    httpclient.getState().setCredentials(AuthScope.ANY, credentials);
    HostConfiguration hostConfiguration = new HostConfiguration();

    int status;/*  w  w w .  j av  a  2 s  .c o m*/
    try {
        status = httpclient.executeMethod(hostConfiguration, method);
    } catch (IllegalArgumentException ex) {
        status = -1;
    }

    Response response = new Response();
    if (status != -1) {
        String body = null;
        String encoding = "";
        Header contentTypeHeader = method.getResponseHeader("Content-Type");
        if (contentTypeHeader != null) {
            String contentType = contentTypeHeader.getValue();
            if (contentType != null && contentType.contains("charset")) {
                encoding = contentType.replaceFirst("^.*;\\s*charset\\s*=\\s*", "").replace("\"", "").trim();
            }
        }

        // get the response
        InputStreamReader inputStreamReader = null;
        if (!Const.isEmpty(encoding)) {
            inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream(), encoding);
        } else {
            inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream());
        }
        StringBuilder bodyBuffer = new StringBuilder();
        int c;
        while ((c = inputStreamReader.read()) != -1) {
            bodyBuffer.append((char) c);
        }
        inputStreamReader.close();
        body = bodyBuffer.toString();

        // Get response time
        long responseTime = System.currentTimeMillis() - startTime;

        response.setStatusCode(status);
        response.setResult(body);
        response.setResponseTime(responseTime);
    }
    return response;
}

From source file:org.pentaho.di.trans.steps.loadfileinput.LoadFileInput.java

/**
 * Read a text file./*from  ww w  . j  a  va 2  s  .  c om*/
 *
 * @param vfsFilename
 *          the filename or URL to read from
 * @param charSetName
 *          the character set of the string (UTF-8, ISO8859-1, etc)
 * @return The content of the file as a String
 * @throws KettleException
 */
public static String getTextFileContent(String vfsFilename, String encoding) throws KettleException {
    InputStream inputStream = null;
    InputStreamReader reader = null;

    String retval = null;
    try {
        inputStream = KettleVFS.getInputStream(vfsFilename);

        if (!Const.isEmpty(encoding)) {
            reader = new InputStreamReader(new BufferedInputStream(inputStream), encoding);
        } else {
            reader = new InputStreamReader(new BufferedInputStream(inputStream));
        }

        int c;
        StringBuffer stringBuffer = new StringBuffer();
        while ((c = reader.read()) != -1) {
            stringBuffer.append((char) c);
        }

        retval = stringBuffer.toString();
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "LoadFileInput.Error.GettingFileContent",
                vfsFilename, e.toString()));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) { /* Ignore */
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) { /* Ignore */
            }
        }
    }

    return retval;
}

From source file:org.guvnor.m2repo.backend.server.GuvnorM2Repository.java

private static String loadPom(final File file) {
    InputStream is = null;/*ww  w  .  j a  v  a  2  s .co  m*/
    InputStreamReader isr = null;
    try {
        is = new FileInputStream(file);
        isr = new InputStreamReader(is, "UTF-8");
        StringBuilder sb = new StringBuilder();
        for (int c = isr.read(); c != -1; c = isr.read()) {
            sb.append((char) c);
        }
        return sb.toString();

    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }

    return null;
}

From source file:org.jdal.gis.google.DirectionsService.java

private String responseToString(Object response) throws IOException {
    Writer writer = new StringWriter();
    InputStreamReader reader = new InputStreamReader((InputStream) response);
    int i;/* ww  w  . j a  va  2s. c  o m*/
    while ((i = reader.read()) != -1)
        writer.write(i);

    return writer.toString();

}

From source file:fr.cph.stock.android.web.Connect.java

protected String connectUrl(String adress) throws IOException {
    String toreturn = null;//from w  ww. j a va 2s.com
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    Log.d(TAG, "adress: " + adress);
    HttpGet get = new HttpGet(adress);
    HttpResponse getResponse = client.execute(get);
    HttpEntity responseEntity = getResponse.getEntity();

    Charset charset = Charset.forName("UTF8");
    InputStreamReader in = new InputStreamReader(responseEntity.getContent(), charset);
    int c = in.read();
    StringBuilder build = new StringBuilder();
    while (c != -1) {
        build.append((char) c);
        c = in.read();
    }
    toreturn = build.toString();
    return toreturn;
}