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.

4382 questions
152
votes
4 answers

In Kotlin, how do I read the entire contents of an InputStream into a String?

I recently saw code for reading entire contents of an InputStream into a String in Kotlin, such as: // input is of type InputStream val baos = ByteArrayOutputStream() input.use { it.copyTo(baos) } val inputAsString = baos.toString() And also: val…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
147
votes
5 answers

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

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…
rover12
  • 2,806
  • 7
  • 27
  • 28
146
votes
6 answers

InputStream from a URL

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…
Whitebear
  • 1,761
  • 2
  • 12
  • 22
145
votes
5 answers

Can you explain the HttpURLConnection connection process?

I am using HTTPURLConnection to connect to a web service. I know how to use HTTPURLConnection but I want to understand how it works. Basically, I want to know the following: On which point does HTTPURLConnection try to establish a connection to the…
Arci
  • 6,647
  • 20
  • 70
  • 98
139
votes
5 answers

How can I read a text file in Android?

I want to read the text from a text file. In the code below, an exception occurs (that means it goes to the catch block). I put the text file in the application folder. Where should I put this text file (mani.txt) in order to read it correctly? …
user1635224
  • 1,623
  • 2
  • 15
  • 15
136
votes
8 answers

Byte[] to InputStream or OutputStream

I have a blob column in my database table, for which I have to use byte[] in my Java program as a mapping and to use this data I have to convert it to InputStream or OutputStream. But I don't know what happens internally when I do so. Can anyone…
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
122
votes
8 answers

FileNotFoundException while getting the InputStream object from HttpURLConnection

I am trying to send a post request to a url using HttpURLConnection (for using cUrl in java). The content of the request is xml and at the end point, the application processes the xml and stores a record to the database and then sends back a…
naiquevin
  • 7,588
  • 12
  • 53
  • 62
118
votes
3 answers

Idiomatic way to convert an InputStream to a String in Scala

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…
bballant
  • 1,425
  • 2
  • 10
  • 8
110
votes
9 answers

Guava equivalent for IOUtils.toString(InputStream)

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…
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
107
votes
13 answers

Determine the size of an InputStream

My current situation is: I have to read a file and put the contents into InputStream. Afterwards I need to place the contents of the InputStream into a byte array which requires (as far as I know) the size of the InputStream. Any ideas? As…
ChronoXIII
  • 1,239
  • 3
  • 10
  • 11
105
votes
4 answers

Reading InputStream as UTF-8

I'm trying to read from a text/plain file over the internet, line-by-line. The code I have right now is: URL url = new URL("http://kuehldesign.net/test.txt"); BufferedReader in = new BufferedReader(new…
Chris Kuehl
  • 4,127
  • 3
  • 16
  • 19
100
votes
5 answers

What is the difference between Reader and InputStream?

What is the difference between Reader and InputStream? And when to use what? If I can use Reader for reading characters why I will use inputstream, I guess to read objects?
sab
  • 9,767
  • 13
  • 44
  • 51
96
votes
8 answers

How can I get a java.io.InputStream from a java.lang.String?

I have a String that I want to use as an InputStream. In Java 1.0, you could use java.io.StringBufferInputStream, but that has been @Deprecrated (with good reason--you cannot specify the character set encoding): This class does not properly…
Jared Oberhaus
  • 14,547
  • 4
  • 56
  • 55
94
votes
8 answers

AmazonS3 putObject with InputStream length example

I am uploading a file to S3 using Java - this is what I got so far: AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials("XX","YY")); List buckets = s3.listBuckets(); s3.putObject(new PutObjectRequest(buckets.get(0).getName(),…
JohnIdol
  • 48,899
  • 61
  • 158
  • 242
93
votes
5 answers

Most efficient way to create InputStream from OutputStream

This page: http://blog.ostermiller.org/convert-java-outputstream-inputstream describes how to create an InputStream from OutputStream: new ByteArrayInputStream(out.toByteArray()) Other alternatives are to use PipedStreams and new threads which is…
Vagif Verdi
  • 4,816
  • 1
  • 26
  • 31