inputstream

Getting InputStream with RestTemplate

半世苍凉 提交于 2019-12-17 15:47:20
问题 I am using URL class to read an InputStream from it. Is there any way I can use RestTemplate for this? InputStream input = new URL(url).openStream(); JsonReader reader = new JsonReader(new InputStreamReader(input, StandardCharsets.UTF_8.displayName())); How can I get InputStream with RestTemplate instead of using URL ? 回答1: You should not get the InputStream directly. RestTemplate is meant to encapsulate processing the response (and request) content. Its strength is handling all the IO and

Converting input stream into bitmap

纵饮孤独 提交于 2019-12-17 15:39:28
问题 I have problems converting a input stream from web into bitmap. Problem occurs only when input image type is .BMP (bitmap). In that case: bitmapFactory.decodeStream returns null . Any hints how to fix this problem or where should I continue my debugging? Platform: Android (Honeycomb) URLConnection conn = url.openConnection(); conn.connect(); inputStream = conn.getInputStream(); bufferedInputStream = new BufferedInputStream(inputStream); bmp = BitmapFactory.decodeStream(bufferedInputStream);

Guava equivalent for IOUtils.toString(InputStream)

感情迁移 提交于 2019-12-17 15:24:16
问题 Apache Commons IO has a nice convenience method IOUtils.toString() to read an InputStream to a String. Since I am trying to move away from Apache Commons and to Guava: is there an equivalent in Guava? I looked at all classes in the com.google.common.io package and I couldn't find anything nearly as simple. Edit: I understand and appreciate the issues with charsets. It just so happens that I know that all my sources are in ASCII (yes, ASCII, not ANSI etc.), so in this case, encoding is not an

What's the fastest way to read from System.in in Java?

感情迁移 提交于 2019-12-17 15:10:54
问题 I am reading bunch of integers separated by space or newlines from the standard in using Scanner(System.in) . Is there any faster way of doing this in Java? 回答1: Is there any faster way of doing this in Java? Yes. Scanner is fairly slow (at least according to my experience). If you don't need to validate the input, I suggest you just wrap the stream in a BufferedInputStream and use something like String.split / Integer.parseInt . A small comparison: Reading 17 megabytes (4233600 numbers)

Given a Java InputStream, how can I determine the current offset in the stream?

ⅰ亾dé卋堺 提交于 2019-12-17 12:16:11
问题 I'd like something like a generic, re-usable getPosition() method that will tell me the number of bytes read from the starting point of the stream. Ideally, I would prefer this to work with all InputStreams, so that I don't have to wrap each and every one of them as I get them from disparate sources. Does such a beast exist? If not, can anyone recommend an existing implementation of a counting InputStream ? 回答1: Take a look at CountingInputStream in the Commons IO package. They have a pretty

Given a Java InputStream, how can I determine the current offset in the stream?

非 Y 不嫁゛ 提交于 2019-12-17 12:16:10
问题 I'd like something like a generic, re-usable getPosition() method that will tell me the number of bytes read from the starting point of the stream. Ideally, I would prefer this to work with all InputStreams, so that I don't have to wrap each and every one of them as I get them from disparate sources. Does such a beast exist? If not, can anyone recommend an existing implementation of a counting InputStream ? 回答1: Take a look at CountingInputStream in the Commons IO package. They have a pretty

ClassLoader getResourceAsStream returns null

こ雲淡風輕ζ 提交于 2019-12-17 10:57:14
问题 My project directory structure (in Eclipse): MyProject/ src/ --> "source directory" on Eclipse's classpath/buildpath com.me.myapp Driver myconfig.txt In Driver , I have the following code: public class Driver { public static void main(String[] args) { InputStream is = ClassLoader.getSystemClassLoader.getResourceAsStream("myconfig.txt"); if(is == null) System.out.println("input stream is null"); else System.out.println("input stream is NOT null :-)"); } } When I run this I get the following

ClassLoader getResourceAsStream returns null

不羁的心 提交于 2019-12-17 10:57:08
问题 My project directory structure (in Eclipse): MyProject/ src/ --> "source directory" on Eclipse's classpath/buildpath com.me.myapp Driver myconfig.txt In Driver , I have the following code: public class Driver { public static void main(String[] args) { InputStream is = ClassLoader.getSystemClassLoader.getResourceAsStream("myconfig.txt"); if(is == null) System.out.println("input stream is null"); else System.out.println("input stream is NOT null :-)"); } } When I run this I get the following

InputStream from a URL

不羁岁月 提交于 2019-12-17 10:33:17
问题 How do I get an InputStream from a URL? for example, I want to take the file at the url wwww.somewebsite.com/a.txt and read it as an InputStream in Java, through a servlet. I've tried InputStream is = new FileInputStream("wwww.somewebsite.com/a.txt"); but what I got was an error: java.io.FileNotFoundException 回答1: Use java.net.URL#openStream() with a proper URL (including the protocol!). E.g. InputStream input = new URL("http://www.somewebsite.com/a.txt").openStream(); // ... See also: Using

Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream?

我的未来我决定 提交于 2019-12-17 07:02:22
问题 I was trying to read a file into an array by using FileInputStream, and an ~800KB file took about 3 seconds to read into memory. I then tried the same code except with the FileInputStream wrapped into a BufferedInputStream and it took about 76 milliseconds. Why is reading a file byte by byte done so much faster with a BufferedInputStream even though I'm still reading it byte by byte? Here's the code (the rest of the code is entirely irrelevant). Note that this is the "fast" code. You can just