InputStream 1 « API « Java I/O Q&A

Java I/O Q&A
2.batch File
3.binary File
4.class file
5.CSV file
10.Excel File
11.File Attribute
14.Media File
17.PDF file
20.text file
22.XML file
Java I/O Q&A » API » InputStream 1 

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

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 ...

2. In Java, how do I read/convert an InputStream to a String?

If you have 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 this to a ...

3. Good design: How to pass InputStreams as argument?

I've got a big file on which I'm opening a FileInputStream. This file contains some files each having an offset from the beginning and a size. Furthermore I've got a ...

4. What's the best way to monitor an InputStream?

I'm reading a file in via apache.commons.FtpClient. This works fine 99.9% of the time but sometimes it just dies in the read() method...

InputStream inStream = ftp.retrieveFileStream(path + file.getName());
String fileAsString = "";

if(inStream == ...

5. 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 = this.getClass().getResourceAsStream(fileName)
When are each one ...

6. How to get SelectableChannel from an InputStream?

I want to read from an with a timeout. Apparently the correct way to do this is to use java.nio.channels.SelectableChannel and java.nio.channels.Selector. Unfortunately, it's not clear how to go from ...

7. How can I get a from a java.lang.String?

I have a String that I want to use as an InputStream. In Java 1.0, you could use, but that has been @Deprecrated (with good reason--you cannot specify the ...

8. How to Cache InputStream for Multiple Use

I have an InputStream of a file and i use apache poi components to read from it like this:

POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
The problem is that i need to use the ...

9. Is there an easy way to create a Java InputStream that consists of several appended files?

I have a "processor" component that can process a single File, InputStream, Reader, or etc. For various reasons, I end up with several large files instead of one huge file. Is there ...

10. Pass InputStream through JNA to C code as a File Pointer

I've got a DLL written in C (that I can't edit) that has some function prototype that looks like

#include <stdio.h>
void foo(FILE *bar);
I'd like to create a JNA interface to the DLL ...

11. starting vlc player in java

Hey! I tried to start vlc player in Java, but somehow it did not word. Any other Prog I tried worked. Plz have a look at my code:

 try {

12. Java InputStream != Readable

I am using java.util.Scanner for things such as nextInt(), and all was working fine as long as I was using a java.lang.Readable (one and only constructor argument). However, when I changed ...

13. Non-Deprecated StringBufferInputStream equivalent

I'm working with LogManager.readConfiguration() which requires an InputStream whose contents I'd like to come from a string. Is there an equivalent of StringBufferInputStream that's not deprecated, ...

14. Java: How do I pipe InputStream to standard out?

Is there an easy (therefore quick) way to accomplish this? Basically just take some input stream, could be something like a socket.getInputStream(), and have the stream's buffer autmoatically redirect to standard ...

15. Turning a remote file into an input stream over HTTP in Java

I have the URL of a text file and I want my Java program to read that text file. But the plot thickens! The file is constantly being appended with new ...

16. File path/name from InputStream

How to obtain File Path/Name from an InputStream in Java ?

17. How to load font from InputStream in SWT?

I need to load font (.otf or .ttf) file from java Resource or InputStream in SWT. path) allows me (example) to load font from font file path (and it works), ...

18. getResourceAsStream returns HttpInputStream not of the entire file

I am having a web application with an applet which will copy a file packed witht the applet to the client machine. When I deploy it to webserver and use: InputStream ...

19. Problem opening/downloading zip-file after decompression (Java/ZipInputStream)

The background
Currently, I try to get a zip-file through a HTTP-servlet with a HTTP-request. The received zip-file contains three different files. These files contain information I want to filter out and ...

20. FileWritter and InputStream in java

Before when I use file in my fileWritter it worked, but now since I'm using getResourceAsStream instead of file how can I make it work?

FileWriter fstream = new FileWriter(new File("filename"), true);
Now ...

21. Process InputStream

How can I "modify" an InputStream? I have a file as input, I would like to modify some variable and forward a new InputStream. For example, the initial InputStream contains Hello ${var}. ...

22. Open a bitmap with Inputstream

How would I open a bitmap with Inputstream? e.g InputStream inputStream = image where image is of type Bitmap? Thanks.

23. Java - communicating with a sub app through the input stream

Is there a way I can start a command-line application from java and then send strings (commands) to its input stream and display its response from its output stream? I'm using an ...

25. Is there a way to mark an InputStream twice?

I want to make it so I can call in.mark(512) to limit my stream to 512 bytes. Then, later I want to limit my stream to 64 bytes. Can I later ...

26. Java input stream limit: protecting against DoS attacks

I'm coding a tool, that, given any URL, would periodically fetch its output. The problem is that an output could be not a simple and lightweight HTML page (expected in most ...

27. how to create RandomAccessFile from InputStream?

The only way I know is to write a temp file, and read this file to create RandomAccessFile ,check it here or use Use Unified I/O library (

28. JRuby: Create Java InputStream or byte[] from RubyString

I have a Java method I want to call from JRuby. The argument I want to pass is just a normal string (org.jruby.RubyString), but the Java method is overloaded to ...

29. Why an InputStream obj cannot be nested into a BufferedReader obj directly?

An OutputStream obj can be connected into a PrintWriter obj directly, e.g.,

//either is OK
new PrintWriter(socket.getOutputStream());
new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
But in the case of an InputStream obj, it must be connected to a BufferedReader ...

30. 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 ...

31. Fast way to compare inputstreams

I have a problem, I need to compare two inputstreams fast. Today I have a function like this:

private boolean isEqual(InputStream i1, InputStream i2) throws IOException {

    try {

32. Create InputStream from String

I'm following this guide, and it gives me the following code:

InputSource inputSource = new InputSource(new FileInputStream(new File("/path/to/xml/file.xml"))));
What I would like to know, is how I can still have the InputSource inputSource, ...

33. efficient input stream to string method in java

So I was running a profiler on my (admittedly pretty simple) application in Java, and was surprised that second only to methods that required making HTTP requests in terms of time ...

34. StringBufferInputStream Question in Java

I want to read an input string and return it as a UTF8 encoded string. SO I found an example on the Oracle/Sun website that used FileInputStream. I didn't want to ...

35. implementing keepalives with Java

I am biulding a client-server application where I have to implement a keepalive mechanism in order to detect that the client has crashed or not. I have separate threads on both ...

36. Right way of formatting an input stream

I have the following issue: my program is passed an InputStream of which I cannot control the contents. I unmarshal my input stream using the javax library, which rightfully throws ...

37. how to use TextArea as an inputStream

I want to use TextArea as an inputStream.Anyone have idea how this can be done...?

38. Translate a byte sequence given an InputStream

In Java, is there a library anywhere that will, given a byte sequence (preferable expressed as hex), translate to another byte sequence given an InputStream? For example:

InputStream input = new ...

39. InputStream.available() doesn't work

I am trying to use inputstream.available () to check if there is any data to read without blocking the thread. but it never return any value > 0. am I using ...

40. OpenSAML bootstrap() nets me "InputStream cannot be null"

Deployment environment: Windows Server 2008, Tomcat 7.0.5, Oracle JRE 1.6. Development environment same plus Eclipse Helios. When I try to deploy my Java application which uses OpenSAML 2.3.1 and test it live, ...

41. Which is the most efficient way of taking input in Java?

I am solving this question. This is my code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {

42. Multiple readers for InputStream in Java

I have an InputStream from which I'm reading characters. I would like multiple readers to access this InputStream. It seems that a reasonable way to achieve this is to write incoming ...

43. How to create a Java non-blocking InputStream from a HttpsURLConnection?

Basically, I have a URL that streams xml updates from a chat room when new messages are posted. I'd like to turn that URL into an InputStream and continue reading ...

44. InputStream.available() is returning 0 or No file size

Please consider the following j2me code segment:

1. FileConnection fc = (FileConnection)"file:///root1/photos/2.png");
2. InputStream is = fc.openInputStream();
3. System.out.println(is.available());
4. byte[] fileBytes = new byte[is.available()];
5. int sizef =;
6. System.out.println("filesize:"+sizef);
In this case line 3 ...

45. using asm (java bytecode) classreader on a class outside of my project packages?

I'm getting by input the path of a class (lets say "C:\Temp\Test.class). What i'd like to do is to intialize a ClassReader to this class and start instrumentation it using ClassAdapter ...

46. Java Wrapping an InputStream

I need to repeatedly read from an InputStream. The stream is from reading some XML from a web service request. I'd like to keep the XML in memory so I may ...

47. InputStream is = App.class.getResourceAsStream("");

the returned InputStream is null. What will be the possible cause?

48. Testing for emtpy InputStream in Java

how do you guys test for an empty InputStream ? I know that InputStream is designed to work with remote resources, so you can't know if it's there until you actually ...

49. API design question on File and InputStream parameter

can I consider File as a special case of InputStream and provide 1 API like

 test(InputStream stream)
or two overloaded APIs are better
test(File f)
test(InputStream stream)

50. Sending a TAB control character in Java

I am using the Ganymede ssh library ( to connect to a ssh server (obviously ;)). Normally, when using the bash, when a user presses the TAB key on the ...

51. How does one create an InputStream from a String?

I'm not used to working with streams in Java - how do I create an InputStream from a String?

52. Get filename from an inputstream (Java)

if I have this code, how could I keep the filename of the original file or reassign it to the new one?:

    InputStream input= assetInfo.openStream();

53. Getting java.lang.StringIndexOutOfBoundsException : String index out of range: (not able to figure it out)

I have a piece of code -

import java.util.Scanner;

import org.eclipse.core.runtime.Platform;

public class RAF {

    public static void main(String[] args) {


54. 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 ...

55. InputStream returns -1 before end of file

I'm streaming a file from a server and then streaming it back out to a socket (basically a stream proxy). This is being used to buffer a song. So ...

56. Java InputStream 0x0A issue

In Java I have the following

while(true) {
            input  = clientSocket.getInputStream();

57. why my program never reach the solve method?

sorry if its a stupid question, but I a beginner using StreamTokenizer, I am trying to solve this exercise this, please help me, I dont know what its wrong ...

58. Apache Tika and File access instead of Java Input Stream

I want to be able to create a new Tika parser to extract metadata from a file. We're already using Tika and the metadata extraction will be done consistently. I think ...

59. How to deal with BOM in InputStream

Possible Duplicate:
Byte order mark screws up file reading in Java
public Collection<String> getLines(String path) throws SftpException
   BufferedReader reader = null;

60. Is this LimitedInputStream correct?

I've written a class called LimitedInputStream. It wraps around an existing input stream to limit the number of bytes read from it to a specified length. It's meant as an alternative ...

61. Java - InputStream - Test for input

I am sending data to a server in two steps: 1) Length of what I will send using byte[4] 2) Data. The server listens to the exact length of the data (shipped first) and ...

62. Recovering a crashed InputStream

I'm writing a server and I'm using an ObjectInputStream to handle proprietary packets. I've recently been sometimes getting some unexpected ClassCastExceptions when I want call objectInputStream.readObject() and then trying casting them ...

63. Mocking Java InputStream

Please provide pointers to help me mock that java InputStream object. This is the line of code that I would wish to Mock:

InputStreamReader inputData = new InputStreamReader(;
bufferdReader = new BufferedReader(inputData);

64. InputStream in java

How to get InputStream and size of File type in servlet? Previously i was using FileUpload type which has getInputStream() and getSize() methods, but now i have to use File type for ...

65. Why is it still storing my previous inputstream

I am going through the inputStream and checking if the first input of the file has the valid input but the problem is if it doesn't I call my method to ...

66. unusual behavior of ClassLoader

public InputStream getResourceAsStream ( Class className, String name){
       InputStream in = null;
       in = className.getClassLoader().getResourceAsStream(name);

67. How to get the InputStream from a files list of rtf document files?

I have a list of richTextFormat documents that I need to loop through and get the InputStream from each file to merge with another document. I used this code to ...

68. How to download/gunzip a file and obtain an InputStream of the actual file?

I have a url of a gzipped file. I would like to obtain an InputStream of the actual file in Java. What is the best way to achieve this?

69. java inputstream

What is an InputStream's available() method is supposed to return when the end of the stream is reached? The documentation doesn't specify the behavior.

70. Creating inputstream from MulticastSocket

I have a method like this: public void readData( aStream); and my data will come from MulticastSocket( For using readData method I have to create an InputStream using MulticastSocket. So my question is: "How can ...

71. io exception in openInputStream in Nokia 5800

I am using the following code to parse XML from server.

try {
    //Open http connection
    HttpConnection httpConnection = (HttpConnection);


72. SSL error while opening input stream

I guess I am trying to understand how SSL works, I have grails based application as part of application process we have to open "https" connection to a 3rd party application. But ...

73. Corrupted Content Error with Firefox 7.0.1

When I am trying to download something through my website using Firefox 7.0.1, I got following Error:

Corrupted Content Error The page you are trying to view ...

74. InputStream initializing via another InputStream in Java

Is it possible to initialize an inputstream via another inputstream in java ?

75. InputStream help

77. ProgressMonitor InputStream

Hi! This has become an unsolvable problem although it does not affect the functionality of the method. The ProgressMonitorStream does not pop up! Not even with mp3 files over 8 meg! I've tried in different ways. If I read the bytes one by one with read() and not with read(byte[],int,int) then it pops up. But that's much slower and seems unnecessary. ...

78. InputStream in = uConnect.getInputStream(); error

Hi, It would be kind if anybody could tell me why i am getting on line InputStream in = uConnect.getInputStream(); ----------------------------------------------- I am created a URL object and passing that URL object in a URLConnection object. String page_to_connect = "http://cd2iis02/inbi/admin/updatestatus.cfm?domain="+sdomain_name+"&requestid="+sreq_id+"&status="+status+"&msg="+ smessage; URL serverUrl = new URL(page_to_connect); URLConnection uConnect = serverUrl.openConnection(); uConnect.setDoOutput(true); uConnect.setUseCaches(false); PrintStream out = new PrintStream(uConnect.getOutputStream()); out.close(); //Filenotfound exception ...

79. About InputStream

Hi I m reading an image from access database. I m using the result set method calles "getBinaryStream" which returns me a InputStream object. Can anyone tell me, what does this Input Stream method contains, and how do i save the image to the hard disk. Thanks [ April 18, 2002: Message edited by: Afroz Khan ]

80. Ignoring alternate Paragraph from an Input Stream

Hai, I am getting an FileInputStream which contains few paragraphs of data, i like to read only alternate paragraphs. my paragraphs should be in input stream only (because my application can process only streams not readers). so how can i mark an empty line in the input stream and how could i ignore the few lines from the input stream. Thanks ...

81. need to reuse input stream

I have a text file object where the constructor takes an input stream. The interface has a getReader() call that returns the reader for this text file object. In my implementation, if I just return a reader using this input stream like: return new InputStreamReader(inputStream); Then its a one time use implementation. The first call returns the reader. Its read until ...

82. inputstream

83. InputStream to multiple Readers

I have a socket server that gets a socket and does this: BufferedReader mReader = new BufferedReader( new InputStreamReader( mSocket.getInputStream() ) ); In response to some comand that comes in over this stream, I want to temporarily hook the socket up to another reader. InputStream mTempInput = mSocket.getInputStream(); Now, ya can't have two consumers hooked up to one provider. Once one ...

84. Why doesn't my InputStream have bytes.

Hi. I built the code bellow to print some characters coming from a InputStream. But, nothing is being printed. I guess my InputStream is empity. What is wrong? ===================== my code: ... String s = new String("javac"); Process processo = Runtime.getRuntime().exec(s); BufferedInputStream in = new BufferedInputStream(processo.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); while ((s = reader.readLine()) != null) { System.out.println(reader.readLine()); ...

85. Adding to an Input Stream

Hi All, I have a stream of a file in the form of a Reader. I pass this Reader to a 'tool' that does something. What I want to do is this: After the stream has been obtained from a file I want to add something to the stream and still be able to pass the Reader to the 'tool' for ...

86. Passing inputstream to a sub class?

If I understand you correctly the code you have shown us is in a super class and you want to pass the string myString to a method in the subclass. Simple. You declare an abstract method in your super class that takes a String as an argument and you implement that method in the subclass.

87. How to get a String from an InputStream

88. Changing a String to an InputStream()

89. Save an InputStream to a file

90. Catching InputStream from a Process

Hi, I am facing a problem that some of you may have also seen in the past. I am trying to catch the InputStream from a Process which is simply Runtime rt = Runtime.getRuntime(); Process pr = rt.exec( "tail -5000000 A_Very_Large_File.txt" ); The problem is that the input stream coming from this process it too fast for my BufferedReader br = ...

91. skip() with InputStream

Hello, I would like to know if there's an effective way to skip already read bytes from an InputStream ?? I would like to write a program which allows to resume downloads. Here is the code I use private void doSkip(RandomAccessFile raf, InputStream is){ try{ long toSkip = raf.length(); long count = 0; long skip = toSkip; long effectiveskip = 0; ...

92. InputStream -> File

93. InputStream to way?

I need to convert from an InputStream to a RandomAccessFile. The reason for this can be found in the thread which prompted this question. That thread can be found here: The file to be read (which is a compressed image file of type .cg) will be read by a servlet. This will pass back to an applet. Anyway, what I ...

94. InputStreams & POST

I admin a website that has an IP tracker installed. It's tedious, as you need to enter each IP individually, and I have to regularly search. So I'm trying to write an application that will take a list of IPs, search, and create a report. Unfortunately I'm stuck just trying to get it to work with the values already set... I'm ...

95. duplicate inputstream

Hi, my server side code is reading inputstream from client. This inputstream i am passing to saxparser. Saxparser is able to parse the document, its working fine. my client may get disconnected. so i have to start resending the complete file to server again. Instead i want to create a temp file and same time i want to send the inputstream ...

96. One query regarding InputStream

Hello, I have a query regarding Input Stream.Its like I am having some "String" Data & have one method for parsing. this parsing method has the signature like this public void parse(InputStream , Some other UserDefined type) Now i want to stream this data to bytes & pass this bytes data to this parse() for its InputStream ,& afterwards i am ...

97. finding a String in an InputStream

Hi everyone, I have an InputStream containing both binary and ASCII datas. I would like to scan through an InputStream to find if it contains a certain String. I was thinking about to convert the String to a byte-Array and to scan through the byte-Array gained of the InputStream in order to replace the matching bytes with a new String. I ...

98. Relation between InputStream and InputSreamReader

Note that there is no class BufferedInputStreamReader in the standard Java libraries. Perhaps you're thinking of one of these: BufferedInputStream BufferedReader InputStreamReader These are discussed in the link Marc gave. If you're really dealing with a class called BufferedInputStreamReader, then it's not a standard class, and we can't really help with what it does without knowing more about where it came ...

99. how to use mark() of InputStream

100. Saving gif file from inputstream

Hello there! I'm using HttpClient to fecth some images (I do have permission for those ) and then save them on filesystem. What I was doing so far is getting the inputstream and saving it: GetMethod method = new GetMethod(""); client.executeMethod(method); writeFile(method.getResponseBodyAsStream(),fileName); private void writeFile(InputStream input, int fileName){ OutputStream out = null; try { out = new FileOutputStream(new File(fileName+".gif")); byte[] buffer ...  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.