List of usage examples for java.io InputStreamReader read
public int read(java.nio.CharBuffer target) throws IOException
From source file:com.xerox.amazonws.common.AWSQueryConnection.java
private String getStringFromStream(InputStream iStr) throws IOException { InputStreamReader rdr = new InputStreamReader(iStr, "UTF-8"); StringWriter wtr = new StringWriter(); char[] buf = new char[1024]; int bytes;/* w w w .j ava 2 s. c o m*/ while ((bytes = rdr.read(buf)) > -1) { if (bytes > 0) { wtr.write(buf, 0, bytes); } } iStr.close(); return wtr.toString(); }
From source file:org.omegat.util.FileUtilTest.java
private String readFile(File file) throws IOException { InputStreamReader stream = new InputStreamReader(new FileInputStream(file)); char[] cbuf = new char[256]; int len;//from w w w . j a v a 2 s. co m StringBuilder sb = new StringBuilder(); while ((len = stream.read(cbuf)) != -1) { sb.append(cbuf, 0, len); } stream.close(); return sb.toString(); }
From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java
public String getMicrophoneSample() { String out = ""; String fileName = "microphone.3gp"; MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(MyApp.context.getFilesDir() + "/" + fileName); try {//from w ww . ja v a2 s . co m recorder.prepare(); recorder.start(); Thread.sleep(5000); recorder.stop(); recorder.release(); File f = new File(MyApp.context.getFilesDir() + "/" + fileName); FileInputStream fileIn = MyApp.context.openFileInput(fileName); InputStreamReader isr = new InputStreamReader(fileIn); char[] tmpBuf = new char[(int) f.length()]; isr.read(tmpBuf); out = new String(tmpBuf); } catch (Exception e) { e.printStackTrace(); } return out; }
From source file:org.acmsl.commons.utils.io.IOUtils.java
/** * Reads given input stream into a String. * @param inputStream the input stream to read. * @param contentLength the length of the content. * @param charset the charset.// w ww .j av a 2 s. com * @return the input stream contents, or an empty string if the operation * fails. * @exception IOException if the input stream cannot be read. */ @NotNull public String read(@NotNull final InputStream inputStream, final int contentLength, @NotNull final Charset charset) throws IOException { @NotNull final StringBuilder t_sbResult = new StringBuilder(); @NotNull final CharUtils t_CharUtils = CharUtils.getInstance(); if (contentLength > 0) { @NotNull final InputStreamReader t_isrReader = new InputStreamReader(inputStream, charset); @NotNull char[] t_acContents = new char[contentLength]; int bytesRead = 0; int totalBytesRead = 0; if (t_isrReader.ready()) { while (totalBytesRead < contentLength) { bytesRead = t_isrReader.read(t_acContents); totalBytesRead += bytesRead; if (bytesRead < contentLength) { @NotNull final char[] aux = new char[bytesRead]; System.arraycopy(t_acContents, 0, aux, 0, bytesRead); t_sbResult.append(aux); t_acContents = new char[contentLength - bytesRead]; } } } t_sbResult.append(t_acContents); } else { @NotNull final InputStreamReader t_isrReader = new InputStreamReader(inputStream, charset); @NotNull final char[] t_acContents = new char[BLOCK_SIZE]; while (t_isrReader.ready()) { final int t_iCharsRead = t_isrReader.read(t_acContents); @NotNull final char[] t_acCharsRead = (t_iCharsRead == BLOCK_SIZE) ? t_acContents : t_CharUtils.subbuffer(t_acContents, 0, t_iCharsRead); t_sbResult.append(t_acCharsRead); } } return t_sbResult.toString(); }
From source file:com.seamusdawkins.autocomplete.MainActivity.java
/** * This method will results show the autocomplete addresses * * @param input/* w w w .j a va 2 s . c o m*/ * @return list */ public ArrayList autocomplete(String input) { ArrayList resultList = null; resultListReference = null; HttpURLConnection conn = null; StringBuilder jsonResults = new StringBuilder(); try { StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON); sb.append("?key=" + activity.getString(R.string.google_server_key)); //sb.append("&components=country:br"); 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.wtf(LOG_TAG, "Error processing Places API URL: " + e); return resultList; } catch (IOException e) { Log.wtf(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(predsJsonArray.length()); resultListReference = new ArrayList(); for (int i = 0; i < predsJsonArray.length(); i++) { System.out.println(predsJsonArray.getJSONObject(i).getString("description")); System.out.println(predsJsonArray.getJSONObject(i).getString("reference")); System.out.println("--------------------------------------------------------"); resultList.add(predsJsonArray.getJSONObject(i).getString("description")); resultListReference.add(predsJsonArray.getJSONObject(i).getString("reference")); } } catch (JSONException e) { Log.wtf(LOG_TAG, "Cannot process JSON results: " + e); } return resultList; }
From source file:markson.visuals.sitapp.settingActivity.java
public String ReadSettings(Context context) { FileInputStream fIn = null;/* w w w .j av a2 s.com*/ InputStreamReader isr = null; char[] inputBuffer = new char[255]; String data = null; try { fIn = openFileInput("settings.dat"); isr = new InputStreamReader(fIn); isr.read(inputBuffer); data = new String(inputBuffer); //Toast.makeText(context, "Classes Loaded",Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "Settings not read", Toast.LENGTH_SHORT).show(); } finally { try { isr.close(); fIn.close(); } catch (IOException e) { e.printStackTrace(); } } return data; }
From source file:it.cicolella.phwswethv2.ProgettiHwSwEthv2.java
private void changeRelayStatus(Board board, Command c) { try {//w w w. j a v a2s. c om URL url = null; URLConnection urlConnection; String delimiter = configuration.getProperty("address-delimiter"); String[] address = c.getProperty("address").split(delimiter); String relayNumber = HexIntConverter.convert(Integer.parseInt(address[1]) - 1); // if required set the authentication if (board.getAuthentication().equalsIgnoreCase("true")) { String authString = board.getUsername() + ":" + board.getPassword(); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); //Create a URL for the desired page url = new URL("http://" + board.getIpAddress() + ":" + board.getPort() + "/protect/" + CHANGE_STATE_RELAY_URL + relayNumber + "=" + c.getProperty("behavior")); urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); } else { //Create a URL for the desired page url = new URL("http://" + board.getIpAddress() + ":" + board.getPort() + "/" + CHANGE_STATE_RELAY_URL + relayNumber + "=" + c.getProperty("behavior")); urlConnection = url.openConnection(); } LOG.info("Freedomotic sends the command " + url); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int numCharsRead; char[] charArray = new char[1024]; StringBuffer sb = new StringBuffer(); while ((numCharsRead = isr.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); } catch (MalformedURLException e) { LOG.severe("Change relay status malformed URL " + e.toString()); } catch (IOException e) { LOG.severe("Change relay status IOexception" + e.toString()); } }
From source file:org.archive.util.Recorder.java
/** * Return a short prefix of the presumed-textual content as a String. * //w w w. j a va2s . c o m * @param size max length of String to return * @return String prefix, or empty String (with logged exception) on any error */ public String getContentReplayPrefixString(int size, Charset cs) { try { InputStreamReader isr = new InputStreamReader(getContentReplayInputStream(), cs); char[] chars = new char[size]; int count = isr.read(chars); isr.close(); if (count > 0) { return new String(chars, 0, count); } else { return ""; } } catch (IOException e) { logger.log(Level.SEVERE, "unable to get replay prefix string", e); return ""; } }
From source file:com.freedomotic.plugins.devices.phwswethv2.ProgettiHwSwEthv2.java
private void changeRelayStatus(Board board, Command c) { try {/*from ww w . j a va 2s.co m*/ URL url = null; URLConnection urlConnection; String delimiter = configuration.getProperty("address-delimiter"); String[] address = c.getProperty("address").split(delimiter); String relayNumber = HexIntConverter.convert(Integer.parseInt(address[1]) - 1); // if required set the authentication if (board.getAuthentication().equalsIgnoreCase("true")) { String authString = board.getUsername() + ":" + board.getPassword(); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); //Create a URL for the desired page url = new URL("http://" + board.getIpAddress() + ":" + board.getPort() + "/protect/" + CHANGE_STATE_RELAY_URL + relayNumber + "=" + c.getProperty("status")); urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); } else { //Create a URL for the desired page url = new URL("http://" + board.getIpAddress() + ":" + board.getPort() + "/" + CHANGE_STATE_RELAY_URL + relayNumber + "=" + c.getProperty("status")); urlConnection = url.openConnection(); } LOG.info("Freedomotic sends the command " + url); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int numCharsRead; char[] charArray = new char[1024]; StringBuffer sb = new StringBuffer(); while ((numCharsRead = isr.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); } catch (MalformedURLException e) { LOG.severe("Change relay status malformed URL " + e.toString()); } catch (IOException e) { LOG.severe("Change relay status IOexception" + e.toString()); } }
From source file:com.freedomotic.plugins.devices.phwswethv2.ProgettiHwSwEthv2.java
private void toggleRelay(Board board, Command c) { try {/* w ww . ja va2 s. co m*/ URL url = null; URLConnection urlConnection; String delimiter = configuration.getProperty("address-delimiter"); String[] address = c.getProperty("address").split(delimiter); String relayNumber = address[1]; int time = Integer.parseInt(c.getProperty("time-in-ms")); int seconds = time / 1000; String relayLine = configuration.getProperty("TOGGLE" + seconds + "S" + relayNumber); // if required set the authentication if (board.getAuthentication().equalsIgnoreCase("true")) { String authString = board.getUsername() + ":" + board.getPassword(); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); //Create a URL for the desired page url = new URL("http://" + board.getIpAddress() + ":" + board.getPort() + "/protect/" + TOGGLE_RELAY_URL + relayLine); urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); } else { //Create a URL for the desired page url = new URL("http://" + board.getIpAddress() + ":" + board.getPort() + "/" + TOGGLE_RELAY_URL + relayLine); urlConnection = url.openConnection(); } LOG.info("Freedomotic sends the command " + url); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int numCharsRead; char[] charArray = new char[1024]; StringBuffer sb = new StringBuffer(); while ((numCharsRead = isr.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); } catch (MalformedURLException e) { LOG.severe("Change relay status malformed URL " + e.toString()); } catch (IOException e) { LOG.severe("Change relay status IOexception" + e.toString()); } }