inputstream

How to programmatically limit the download speed?

不想你离开。 提交于 2019-12-19 07:27:03
问题 I use the following code to limit the download speed of a file in java: package org; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; class MainClass { public static void main(String[] args) { download("https://speed.hetzner.de/100MB.bin"); } public static void download(String link) { try { URL url = new URL(link); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5000); con.setReadTimeout

Getting an InputStream to read more than once, regardless of markSupported()

让人想犯罪 __ 提交于 2019-12-19 05:51:09
问题 I need to be able to re-use a java.io.InputStream multiple times, and I figured the following code would work, but it only works the first time. Code public class Clazz { private java.io.InputStream dbInputStream, firstDBInputStream; private ArrayTable db; public Clazz(java.io.InputStream defDB) { this.firstDBInputStream = defDB; this.dbInputStream = defDB; if (db == null) throw new java.io.FileNotFoundException("Could not find the database at " + db); if (dbInputStream.markSupported())

Getting an InputStream to read more than once, regardless of markSupported()

大憨熊 提交于 2019-12-19 05:50:53
问题 I need to be able to re-use a java.io.InputStream multiple times, and I figured the following code would work, but it only works the first time. Code public class Clazz { private java.io.InputStream dbInputStream, firstDBInputStream; private ArrayTable db; public Clazz(java.io.InputStream defDB) { this.firstDBInputStream = defDB; this.dbInputStream = defDB; if (db == null) throw new java.io.FileNotFoundException("Could not find the database at " + db); if (dbInputStream.markSupported())

Java resource closing

有些话、适合烂在心里 提交于 2019-12-19 05:45:09
问题 I'm writing an app that connect to a website and read one line from it. I do it like this: try{ URLConnection connection = new URL("www.example.com").openConnection(); BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); String response = rd.readLine(); rd.close(); }catch (Exception e) { //exception handling } Is it good? I mean, I close the BufferedReader in the last line, but I do not close the InputStreamReader. Should I create a standalone

Android openRawResource() not working for a drawable

混江龙づ霸主 提交于 2019-12-18 20:29:28
问题 I'm trying to create an input stream by doing this InputStream is = (InputStream) getResources().openRawResource(R.drawable.image1); but I'm met with the error "Expected resource of type raw" with respect to my drawable file (R.drawable.image1). image1 is a png and in my res/drawable folder. Any ideas??? 回答1: @Broatian I don't currently have a res/raw folder. I found an alternative solution: is = context.getResources().openRawResource(+ R.drawable.image1); The + shows additional folders.

Read integers separated with whitespace into int[] array

爷,独闯天下 提交于 2019-12-18 11:13:42
问题 I read line with BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader.readLine(); Example input is 1 4 6 32 5 What is the fastest way to read the input and put it into an integer array int[] ? I'm also looking for some one-line solution if possible. 回答1: You could use Scanner : Scanner scanner = new Scanner(System.in); List<Integer> list = new ArrayList<Integer>(); while (scanner.hasNextInt()) list.add(scanner.nextInt()); int[] arr = list.toArray(new int[0]);

Idiomatic way to convert an InputStream to a String in Scala

我只是一个虾纸丫 提交于 2019-12-18 10:14:28
问题 I have a handy function that I've used in Java for converting an InputStream to a String. Here is a direct translation to Scala: def inputStreamToString(is: InputStream) = { val rd: BufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8")) val builder = new StringBuilder() try { var line = rd.readLine while (line != null) { builder.append(line + "\n") line = rd.readLine } } finally { rd.close } builder.toString } Is there an idiomatic way to do this in scala? 回答1: For Scala >= 2

Can we convert a byte array into an InputStream in Java?

此生再无相见时 提交于 2019-12-18 10:00:43
问题 Can we convert a byte array into an InputStream in Java? I have been looking on the internet but couldn't find it. I have a method that has an InputStream as argument. The InputStream cph I have is base64 encoded so I had to decode it using BASE64Decoder decoder = new BASE64Decoder(); byte[] decodedBytes = decoder.decodeBuffer(cph); Now how do I convert decodedBytes again to InputStream ? 回答1: Use ByteArrayInputStream: InputStream is = new ByteArrayInputStream(decodedBytes); 回答2: If you use

Android issues with AsyncTask and InputStream

帅比萌擦擦* 提交于 2019-12-18 09:45:48
问题 I've been trying to figure this out on my own for quite a while.. by trial/error as well as research, but I just can't seem to get it figured out. I'm pretty bad with Async and network connections and stuch, so it might be something simple that I'm over looking. Anyway... I'll paste some relevant code samples and explanations. Quick background of my problem. I'm working with the Rotten Tomatoes API for my app, and am using GSON for the parsing of their data. I was initially targeting 2.3, and

Java InputStream size

…衆ロ難τιáo~ 提交于 2019-12-18 06:08:49
问题 I need to get the size in bytes of an InputStream without creating a File instance. Is there any way to do it using Java NIO? 回答1: Of a general InputStream ? You'd have to just keep reading and reading (e.g. into the same buffer, over and over) counting how many bytes you've read, until you come to the end of the stream. Of course, you then won't be able to read the data itself... if you want to do that, you'll need to keep the data as you read it, e.g. by copying it to a