Questions tagged [inputstream]

An abstract mechanism for reading a data stream in Java

An InputStream is an abstract mechanism for reading a data stream in Java. A stream is usually comprised of data obtained from a local file, or data being transmitted from a remote server over a network protocol such as HTTP or FTP.

An InputStream is abstract, and doesn't describe the type of data that is being read. Typically you would implement one of the subclasses instead, such as FileInputStream, which more appropriately describes the type of data being handled, and provides additional methods appropriate for that data type.

Streamed data is typically consumed as it is read. In other words, you read the data from the stream starting from the first byte and ending at the last byte. You are usually able to skip over bytes that you don't want to handle, but you can't typically go back to a previous position in the stream, unless you implement some kind of buffering mechanism. In general, you should treat all streamed data as being once-only, and thus you should manually retain any information that you wish to refer to later.

4165 questions
4332
votes
60 answers

How do I read / convert an InputStream into a String in Java?

If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file. What is…
Johnny Maelstrom
  • 44,165
  • 5
  • 19
  • 18
967
votes
16 answers

Creating a byte array from a stream

What is the prefered method for creating a byte array from an input stream? Here is my current solution with .NET 3.5. Stream s; byte[] b; using (BinaryReader br = new BinaryReader(s)) { b = br.ReadBytes((int)s.Length); } Is it still a…
Bob
  • 90,304
  • 29
  • 114
  • 125
911
votes
5 answers

How do I convert a String to an InputStream in Java?

Given a string: String exampleString = "example"; How do I convert it to an InputStream?
Iain
  • 9,533
  • 16
  • 52
  • 58
906
votes
34 answers

Convert InputStream to byte array in Java

How do I read an entire InputStream into a byte array?
JGC
  • 11,599
  • 8
  • 31
  • 53
410
votes
12 answers

How to convert an Stream into a byte[] in C#?

Is there a simple way or method to convert an Stream into a byte[] in C#?
pupeno
  • 256,034
  • 114
  • 324
  • 541
362
votes
12 answers

How to convert OutputStream to InputStream?

I am on the stage of development, where I have two modules and from one I got output as a OutputStream and second one, which accepts only InputStream. Do you know how to convert OutputStream to InputStream (not vice versa, I mean really this way)…
Waypoint
  • 15,705
  • 36
  • 110
  • 167
362
votes
12 answers

byte[] to file in Java

With Java: I have a byte[] that represents a file. How do I write this to a file (ie. C:\myfile.pdf) I know it's done with InputStream, but I can't seem to work it out.
elcool
  • 5,551
  • 7
  • 26
  • 43
302
votes
9 answers

What is InputStream & Output Stream? Why and when do we use them?

Someone explain to me what InputStream and OutputStream are? I am confused about the use cases for both InputStream and OutputStream. If you could also include a snippet of code to go along with your explanation, that would be great. Thanks!
Bohemian
  • 5,777
  • 11
  • 36
  • 47
228
votes
6 answers

Different ways of loading a file as an InputStream

What's the difference between: InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName) and InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) and InputStream is =…
zqudlyba
  • 2,303
  • 3
  • 15
  • 7
203
votes
12 answers

How to create streams from string in Node.Js?

I am using a library, ya-csv, that expects either a file or a stream as input, but I have a string. How do I convert that string into a stream in Node?
pathikrit
  • 29,060
  • 33
  • 127
  • 206
201
votes
18 answers

Capture characters from standard input without waiting for enter to be pressed

I can never remember how I do this because it comes up so infrequently for me. But in C or C++, what is the best way to read a character from standard input without waiting for a newline (press enter). Also ideally it wouldn't echo the input…
Adam
  • 24,261
  • 23
  • 72
  • 87
178
votes
9 answers

How to clone an InputStream?

I have a InputStream that I pass to a method to do some processing. I will use the same InputStream in other method, but after the first processing, the InputStream appears be closed inside the method. How I can clone the InputStream to send to the…
Renato Dinhani
  • 30,005
  • 49
  • 125
  • 194
158
votes
3 answers

Convert InputStream to BufferedReader

I'm trying to read a text file line by line using InputStream from the assets directory in Android. I want to convert the InputStream to a BufferedReader to be able to use the readLine(). I have the following code: InputStream is; is =…
karse23
  • 3,705
  • 5
  • 27
  • 33
150
votes
8 answers

Is it possible to read from a InputStream with a timeout?

Specifically, the problem is to write a method like this: int maybeRead(InputStream in, long timeout) where the return value is the same as in.read() if data is available within 'timeout' milliseconds, and -2 otherwise. Before the method returns,…
JKL
143
votes
11 answers

Read input stream twice

How do you read the same inputstream twice? Is it possible to copy it somehow? I need to get a image from web, save it locally and then return the saved image. I just thought it would be faster to use the same stream instead of starting a new stream…
Warpzit
  • 27,293
  • 18
  • 98
  • 146
1
2 3
99 100