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(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:org.olat.modules.qpool.ui.TextPreviewController.java

protected String readSummary(VFSLeaf leaf) {
    StringWriter out = new StringWriter();
    InputStream in = leaf.getInputStream();
    InputStreamReader inr = new InputStreamReader(in);
    try {//from   www.j  a va2 s  .  c o  m
        char[] buffer = new char[4096];

        int count = 0;
        int n = 0;
        while (-1 != (n = inr.read(buffer))) {
            out.write(buffer, 0, n);
            count += n;
            if (count >= 10000) {
                break;
            }
        }
    } catch (Exception e) {
        logError("", e);
    } finally {
        IOUtils.closeQuietly(inr);
        IOUtils.closeQuietly(in);
    }
    return out.toString();
}

From source file:main.server.DemoPageProvider.java

/**
 * Get the XML for the page or null if page is not found.
 *//*  w ww . j  a  v  a2 s.c om*/
public String getPageXML(PageRequest req) throws IOException {
    InputStream in = null;
    char[] buf = new char[4096];
    StringBuffer xml = new StringBuffer();
    try {
        in = openPageStream(req);
        if (in == null) {
            return null;
        }
        InputStreamReader r = new InputStreamReader(in);
        for (;;) {
            int sz = r.read(buf);
            if (sz < 0) {
                break;
            }
            xml.append(buf, 0, sz);
        }
    } finally {
        close(in);
    }
    return xml.toString();
}

From source file:org.eclipse.orion.server.docker.server.DockerFile.java

/**
 * Get the content of the Dockerfile template located in this bundle.
 * /*from w  w  w.j  av  a  2  s  .co  m*/
 * @return the content of the Dockerfile template.
 */
protected String getDockerfileContent() {
    try {
        String dockerfileName = "Dockerfile.user";
        URL dockerFileURL = Platform.getBundle("org.eclipse.orion.server.docker").getEntry(dockerfileName);
        File dockerfile = new File(FileLocator.toFileURL(dockerFileURL).getPath());

        FileInputStream fileInputStream = new FileInputStream(dockerfile);
        char[] chars = new char[(int) dockerfile.length()];
        Charset utf8 = Charset.forName("UTF-8");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, utf8);
        inputStreamReader.read(chars);
        inputStreamReader.close();
        fileInputStream.close();

        String result = new String(chars);
        result = result.replaceAll("USERNAME", userName);
        result = result.replaceAll("UID", userId);
        result = result.replaceAll("GID", groupId);
        return result;
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
    }
    return null;
}

From source file:org.springframework.configurationmetadata.JsonReader.java

private JSONObject readJson(InputStream in, Charset charset) throws IOException {
    try {/* ww  w  .j a v  a 2  s .c  om*/
        StringBuilder out = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(in, charset);
        char[] buffer = new char[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = reader.read(buffer)) != -1) {
            out.append(buffer, 0, bytesRead);
        }
        return new JSONObject(out.toString());
    } finally {
        in.close();
    }
}

From source file:com.ibm.liberty.starter.service.watsonsdk.api.v1.it.EndpointTest.java

private String inputStreamToString(InputStream inputStream) throws IOException {
    InputStreamReader isr = new InputStreamReader(inputStream);
    char[] chars = new char[1024];
    StringBuilder responseBuilder = new StringBuilder();

    int read;/*from w  w  w .  j av a2 s.c om*/
    while ((read = isr.read(chars)) != -1) {
        responseBuilder.append(chars, 0, read);
    }
    return responseBuilder.toString();
}

From source file:com.zaa.WeatherLoad.java

public String convertStreamToString(InputStream is) throws IOException {
    InputStreamReader r = new InputStreamReader(is);
    StringWriter sw = new StringWriter();
    char[] buffer = new char[1024];
    try {//from w w  w .j  a v a2s .c om
        for (int n; (n = r.read(buffer)) != -1;)
            sw.write(buffer, 0, n);
    } finally {
        try {
            is.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return sw.toString();
}

From source file:dk.statsbiblioteket.alto.AnagramHashing.java

private String read(File alto) throws IOException {
    final char[] BUF = new char[4096];

    FileInputStream in = new FileInputStream(alto);
    InputStreamReader inr = new InputStreamReader(in, "utf-8");
    StringBuilder sb = new StringBuilder();
    int r;//w  w  w  . j av  a  2s  . c o  m
    while ((r = inr.read(BUF)) != -1) {
        sb.append(BUF, 0, r);
    }
    return sb.toString();
}

From source file:org.alfresco.module.vti.web.actions.VtiWelcomeInfoAction.java

/**
 * <p>Return the information to determine the entry point for 
 * the Microsoft FrontPage Server Extensions.</p> 
 *
 * @param request HTTP request/*from  www  .j  av a2 s  .  com*/
 * @param response HTTP response
 */
public void execute(HttpServletRequest request, HttpServletResponse response) {
    // Build the model
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("alfrescoUrl", UrlUtil.getAlfrescoUrl(sysAdminParams));
    model.put("shareUrl", UrlUtil.getShareUrl(sysAdminParams));
    model.put("vtiUrl", getVtiUrl(request));

    // Get the welcome template
    InputStream templateStream = getClass().getResourceAsStream(template);
    if (templateStream == null) {
        logger.warn("Welcome template missing: " + template);
        return;
    }
    StringBuilder template = new StringBuilder();
    try {
        InputStreamReader r = new InputStreamReader(templateStream, Charset.forName("UTF-8"));
        int read;
        char[] c = new char[4096];
        while ((read = r.read(c)) != -1) {
            template.append(c, 0, read);
        }
        r.close();
    } catch (IOException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Action IO exception", e);
        }
    } finally {
        try {
            templateStream.close();
        } catch (Exception e) {
        }
    }

    try {
        String res = templateService.processTemplateString("freemarker", template.toString(), model);
        PrintWriter r = response.getWriter();
        r.append(res);
        r.close();
    } catch (IOException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Action IO exception", e);
        }
    }
}

From source file:oss.ridendivideapp.AutoCompleteAPI.java

public ArrayList<String> autocomplete(String input) {
    ArrayList<String> resultList = null;

    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {//from  w w w .  j a  va2 s  .c o m
        /* Code to form the URL and query against Autocomplete API via a http request */
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        sb.append("?&types=geocode&language=en&sensor=true&key=" + API_KEY);
        sb.append("&input=" + URLEncoder.encode(input, "utf8"));
        URL url = new URL(sb.toString());

        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());

        /* Load the results into a StringBuilder */
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "Error processing Places API URL", e);
        return resultList;
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error connecting to Places API", e);
        return resultList;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }

    try {
        /* Create a JSON object hierarchy from the results */
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");

        /* Extract the Place descriptions from the results */
        resultList = new ArrayList<String>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Cannot process JSON results", e);
    }

    return resultList;
}

From source file:org.cesar.geofencesdemo.map.MapSearchAutocompletion.java

@Override
protected ArrayList<String> doInBackground(final Void... params) {

    ArrayList<String> resultList = null;

    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {//from  w  ww .ja v  a  2s .  c  o  m
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        sb.append("?sensor=false&key=" + API_KEY);
        sb.append("&input=" + URLEncoder.encode(mInput, "utf8"));

        URL url = new URL(sb.toString());
        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());

        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }

    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "Error processing Places API URL");
        e.printStackTrace();
        return resultList;
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error connecting to Places API");
        e.printStackTrace();
        return resultList;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }

    try {
        // Create a JSON object hierarchy from the results
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");

        // Extract the Place descriptions from the results
        resultList = new ArrayList<String>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Cannot process JSON results");
        e.printStackTrace();
    }

    return resultList;
}