public void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
@Override public synchronized void reset() throws IOException { if (!in.markSupported()) { throw new IOException("Mark not supported"); } if (mark == -1) { throw new IOException("Mark not set"); } in.reset(); count = mark; } }
private String getMmsText(String id) { Uri partURI = Uri.parse("content://mms/part/" + id); InputStream is = null; StringBuilder sb = new StringBuilder(); try { is = getContentResolver().openInputStream(partURI); if (is != null) { InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader reader = new BufferedReader(isr); String temp = reader.readLine(); while (temp != null) { sb.append(temp); temp = reader.readLine(); } } } catch (IOException e) {} finally { if (is != null) { try { is.close(); } catch (IOException e) {} } } return sb.toString(); }
HttpURLConnection connection = null; try { URL url = new URL(sUrl[0]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage(); int fileLength = connection.getContentLength(); input = connection.getInputStream(); output = new FileOutputStream("/sdcard/file_name.extension"); while ((count = input.read(data)) != -1) { input.close(); return null; output.write(data, 0, count); try { if (output != null) output.close(); if (input != null) input.close(); } catch (IOException ignored) {
ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); try { URL url = new URL(urlToDownload); URLConnection connection = url.openConnection(); connection.connect(); int fileLength = connection.getContentLength(); InputStream input = new BufferedInputStream(connection.getInputStream()); OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk"); while ((count = input.read(data)) != -1) { total += count; resultData.putInt("progress" ,(int) (total * 100 / fileLength)); receiver.send(UPDATE_PROGRESS, resultData); output.write(data, 0, count); output.flush(); output.close(); input.close(); } catch (IOException e) { e.printStackTrace();
protected final String sendPostRequest(String request) throws IOException { URL url = new URL("http", DEFAULT_HOST, this.port, request); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); OutputStream out = con.getOutputStream(); out.write("".getBytes()); out.flush(); InputStream in = con.getInputStream(); String response = CharStreams.toString(new InputStreamReader(con.getInputStream())); in.close(); return response; }
public void writeRequest(final OutputStream out) throws IOException { byte[] tmp = new byte[4096]; int i = 0; InputStream instream = new FileInputStream(this.file); try { while ((i = instream.read(tmp)) >= 0) { out.write(tmp, 0, i); } } finally { instream.close(); } }
public void publicizeResources(File arscFile) throws AndrolibException { byte[] data = new byte[(int) arscFile.length()]; try(InputStream in = new FileInputStream(arscFile); OutputStream out = new FileOutputStream(arscFile)) { in.read(data); publicizeResources(data); out.write(data); } catch (IOException ex){ throw new AndrolibException(ex); } }
InputStream in = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); // The other side says hello: String text = br.readLine(); // For whatever reason, you want to read one single byte from the stream, // That single byte, just after the newline: byte b = (byte) in.read();
public static String readStringFromStdin(String message) throws Exception { System.out.print(message); System.out.flush(); System.in.skip(System.in.available()); BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); String line=reader.readLine(); return line != null? line.trim() : null; }
if (extractedFile.exists()) { try { extractedCrc = crc(new FileInputStream(extractedFile)); } catch (FileNotFoundException ignored) { input = readFile(sourcePath); extractedFile.getParentFile().mkdirs(); output = new FileOutputStream(extractedFile); byte[] buffer = new byte[4096]; while (true) { int length = input.read(buffer); if (length == -1) break; output.write(buffer, 0, length);
/** * Downloads the plugin JAR from the given URL to a temporary * local location. */ private File downloadPluginJar(String pluginJarUrl) throws Exception { URL pluginJarFile = new URL(pluginJarUrl); logger.log(Level.INFO, "Querying " + pluginJarFile + " ..."); URLConnection urlConnection = pluginJarFile.openConnection(); urlConnection.setConnectTimeout(2000); urlConnection.setReadTimeout(2000); File tempPluginFile = File.createTempFile("syncany-plugin", "tmp"); tempPluginFile.deleteOnExit(); logger.log(Level.INFO, "Downloading to " + tempPluginFile + " ..."); FileOutputStream tempPluginFileOutputStream = new FileOutputStream(tempPluginFile); InputStream remoteJarFileInputStream = urlConnection.getInputStream(); IOUtils.copy(remoteJarFileInputStream, tempPluginFileOutputStream); remoteJarFileInputStream.close(); tempPluginFileOutputStream.close(); if (!tempPluginFile.exists() || tempPluginFile.length() == 0) { throw new Exception("Downloading plugin file failed, URL was " + pluginJarUrl); } return tempPluginFile; }
private void download(final URL url, final File file) throws IOException { LOG.info("Downloading zip data set to " + file); InputStream inputStream = url.openStream(); FileOutputStream outputStream = new FileOutputStream(file); try { byte[] read = new byte[49152]; int count; while ((count = inputStream.read(read)) != -1) { outputStream.write(read, 0, count); } } finally { inputStream.close(); outputStream.close(); } }
InputStream is = getResources().openRawResource(R.raw.json_file); Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } String jsonString = writer.toString();
URL url; try { url = new URL(apiUrl); } catch (MalformedURLException e) { throw new RuntimeException(e); SearchResult result = new SearchResult(); try { urlConnection = (HttpURLConnection) url.openConnection(); is = urlConnection.getInputStream(); InputStreamReader reader = new InputStreamReader(is); result = new Gson().fromJson(reader, SearchResult.class); } catch (IOException e) { if (is != null) { try { is.close(); } catch (IOException e) { urlConnection.disconnect();
/** * get md5. * * @param file file source. * @return MD5 byte array. */ public static byte[] getMD5(File file) throws IOException { InputStream is = new FileInputStream(file); try { return getMD5(is); } finally { is.close(); } }
Properties properties = new Properties(); InputStream inputStream = new FileInputStream("path/to/file"); try { Reader reader = new InputStreamReader(inputStream, "UTF-8"); try { properties.load(reader); } finally { reader.close(); } } finally { inputStream.close(); }
in = url.openStream(); out = new FileOutputStream(tmpFile); if (TRY_TO_PATCH_SHADED_ID && PlatformDependent.isOsx() && !packagePrefix.isEmpty()) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(in.available()); while ((length = in.read(buffer)) > 0) { byteArrayOutputStream.write(buffer, 0, length); out.write(bytes); } else { while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); out.flush();
public void run() { try { for(int i=0;i<10;i++) { URL url = new URL("http://192.168.220.130/index.html"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStream in = conn.getInputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = 0; while((len = in.read(buff)) >= 0) { bout.write(buff, 0, len); } in.close(); bout.close(); byte[] response = bout.toByteArray(); System.out.println(new String(response, "UTF-8")); Thread.sleep(3000); } }catch(Exception e) { } } });