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.openadaptor.util.FileUtils.java

/**
 * @param fstream/*from  w w  w.  j  a  va2 s. co m*/
 *          the stream used to access the file contents
 * 
 * @return the contents of the file referenced by the input stream supplied. The file is processed in 1K chunks.
 * 
 * @throws IOException
 *           if the file doe not exist
 */
public static String getFileContents(InputStream fstream) throws IOException {
    InputStreamReader in = new InputStreamReader(fstream);

    StringWriter writer = new StringWriter();
    char[] buf = new char[1024];
    int count;

    while ((count = in.read(buf)) != -1)
        writer.write(buf, 0, count);

    in.close();
    fstream.close();

    return writer.toString();
}

From source file:org.pentaho.di.cluster.HttpUtil.java

/**
 * Base 64 decode, unzip and extract text using {@link Const#XML_ENCODING} predefined charset value for byte-wise
 * multi-byte character handling./*w w w . java  2 s.c o  m*/
 *
 * @param loggingString64
 *          base64 zip archive string representation
 * @return text from zip archive
 * @throws IOException
 */
public static String decodeBase64ZippedString(String loggingString64) throws IOException {
    if (loggingString64 == null || loggingString64.isEmpty()) {
        return "";
    }
    StringWriter writer = new StringWriter();
    // base 64 decode
    byte[] bytes64 = Base64.decodeBase64(loggingString64.getBytes());
    // unzip to string encoding-wise
    ByteArrayInputStream zip = new ByteArrayInputStream(bytes64);

    GZIPInputStream unzip = null;
    InputStreamReader reader = null;
    BufferedInputStream in = null;
    try {
        unzip = new GZIPInputStream(zip, HttpUtil.ZIP_BUFFER_SIZE);
        in = new BufferedInputStream(unzip, HttpUtil.ZIP_BUFFER_SIZE);
        // PDI-4325 originally used xml encoding in servlet
        reader = new InputStreamReader(in, Const.XML_ENCODING);
        writer = new StringWriter();

        // use same buffer size
        char[] buff = new char[HttpUtil.ZIP_BUFFER_SIZE];
        for (int length = 0; (length = reader.read(buff)) > 0;) {
            writer.write(buff, 0, length);
        }
    } finally {
        // close resources
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                // Suppress
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // Suppress
            }
        }
        if (unzip != null) {
            try {
                unzip.close();
            } catch (IOException e) {
                // Suppress
            }
        }
    }
    return writer.toString();
}

From source file:com.dycody.android.idealnote.utils.GeocodeHelper.java

public static List<String> autocomplete(String input) {
    String MAPS_API_KEY = BuildConfig.MAPS_API_KEY;
    if (TextUtils.isEmpty(MAPS_API_KEY)) {
        return Collections.emptyList();
    }//from w w  w .j av a 2 s  .  c  om
    ArrayList<String> resultList = null;

    HttpURLConnection conn = null;
    InputStreamReader in = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        URL url = new URL(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON + "?key=" + MAPS_API_KEY + "&input="
                + URLEncoder.encode(input, "utf8"));
        conn = (HttpURLConnection) url.openConnection();
        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(Constants.TAG, "Error processing Places API URL");
        return null;
    } catch (IOException e) {
        Log.e(Constants.TAG, "Error connecting to Places API");
        return null;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                Log.e(Constants.TAG, "Error closing address autocompletion InputStream");
            }
        }
    }

    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<>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
        }
    } catch (JSONException e) {
        Log.e(Constants.TAG, "Cannot process JSON results", e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
        SystemHelper.closeCloseable(in);
    }
    return resultList;
}

From source file:pt.ubi.di.pdm.swipe.CollectionDemoActivity.java

public static String ReadBtn(Context ctx, String file) {
    //reading text from file
    InputStreamReader InputRead = null;
    String s = "";
    try {//from  w  ww.  j  a v  a 2  s . com
        FileInputStream fileIn = ctx.openFileInput(file);
        InputRead = new InputStreamReader(fileIn);

        char[] inputBuffer = new char[100];

        int charRead;

        while ((charRead = InputRead.read(inputBuffer)) > 0) {
            // char to string conversion
            String readstring = String.copyValueOf(inputBuffer, 0, charRead);
            s += readstring;
        }

        //Toast.makeText(ctx, s,Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        Toast.makeText(ctx, "Nao consegui carregar ficheiro", Toast.LENGTH_SHORT).show();
        //e.printStackTrace();
    } finally {
        if (InputRead != null) {
            try {
                InputRead.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return s;
    }
}

From source file:at.tugraz.sss.serv.SSFileU.java

public static String readStreamText(final InputStream streamIn) throws Exception {

    if (SSObjU.isNull(streamIn)) {
        throw new Exception("pars not okay");
    }/*from   w  w w .  ja va  2  s. c om*/

    try {
        final InputStreamReader inputReader = new InputStreamReader(streamIn, SSEncodingU.utf8.toString());
        final char[] buffer = new char[1];
        String result = new String();

        while (inputReader.read(buffer) != -1) {
            result += buffer[0];
        }

        return result;
    } catch (Exception error) {
        throw error;
    } finally {

        if (streamIn != null) {
            streamIn.close();
        }
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.PostServletImportTest.java

static String getStreamAsString(InputStream is, String charset) throws IOException {
    InputStreamReader reader = new InputStreamReader(is, charset);
    final StringBuilder content = new StringBuilder();
    final char[] buffer = new char[16384];
    int n = 0;//from   ww w . jav a2s  .c  o  m
    while ((n = reader.read(buffer)) > 0) {
        content.append(buffer, 0, n);
    }
    return content.toString();
}

From source file:com.example.android.networkconnect.MainActivity.java

public static String getStringFromInputStream(InputStream stream) throws IOException {
    int n = 0;/*from  w  w w  .  j ava2  s.c om*/
    char[] buffer = new char[1024 * 4];
    InputStreamReader reader = new InputStreamReader(stream, "UTF8");
    StringWriter writer = new StringWriter();
    while (-1 != (n = reader.read(buffer)))
        writer.write(buffer, 0, n);
    return writer.toString();
}

From source file:forge.quest.io.QuestDataIO.java

/**
 * <p>/*from  w w  w  .j av  a2 s  . c o m*/
 * loadData.
 * </p>
 *
 * @param xmlSaveFile
 *            &emsp; {@link java.io.File}
 * @return {@link forge.quest.data.QuestData}
 */
public static QuestData loadData(final File xmlSaveFile) {
    try {
        QuestData data;

        final GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
        final StringBuilder xml = new StringBuilder();
        final char[] buf = new char[1024];
        final InputStreamReader reader = new InputStreamReader(zin);
        while (reader.ready()) {
            final int len = reader.read(buf);
            if (len == -1) {
                break;
            } // when end of stream was reached
            xml.append(buf, 0, len);
        }

        zin.close();

        String bigXML = xml.toString();
        data = (QuestData) QuestDataIO.getSerializer(true).fromXML(bigXML);

        if (data.getVersionNumber() != QuestData.CURRENT_VERSION_NUMBER) {
            try {
                QuestDataIO.updateSaveFile(data, bigXML, xmlSaveFile.getName().replace(".dat", ""));
            } catch (final Exception e) {
                //BugReporter.reportException(e);
                throw new RuntimeException(e);
            }
        }

        return data;
    } catch (final Exception ex) {
        //BugReporter.reportException(ex, "Error loading Quest Data");
        throw new RuntimeException(ex);
    }
}

From source file:com.fiorano.openesb.application.DmiObject.java

public static String getContents(InputStream inputStream) throws IOException {

    StringBuilder builder = new StringBuilder(200);
    InputStreamReader reader = new InputStreamReader(inputStream);
    char[] temp = new char[8 * 1024];
    int readBytes;
    do {//from  www. j a v  a  2 s  . co m
        readBytes = reader.read(temp);
        if (readBytes > 0)
            builder.append(temp, 0, readBytes);
    } while (readBytes > 0);

    return builder.toString();
}

From source file:at.tugraz.sss.serv.util.SSFileU.java

public static String readStreamText(final InputStream streamIn) throws SSErr {

    try {//from  w  w w  .java  2  s .co  m

        final InputStreamReader inputReader = new InputStreamReader(streamIn, SSEncodingU.utf8.toString());
        final char[] buffer = new char[1];
        String result = new String();

        while (inputReader.read(buffer) != -1) {
            result += buffer[0];
        }

        return result;
    } catch (Exception error) {
        SSServErrReg.regErrThrow(error);
        return null;
    } finally {

        if (streamIn != null) {
            try {
                streamIn.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }
    }
}