inputstream

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 more appropriate to use tha...

How to get SelectableChannel from an InputStream?

I want to read from an java.io.InputStream 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 an InputStream to a SelectableChannel. The InputStream is coming from a non-conventional source -- http://java.sun.com...

Getting the inputstream from a classpath resource (XML file)

In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it? ...

Is it possible to read from a Java 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, any spawned threads must exit. To avoid arguments, the subject here java.io.InputStream...

ZipInputStream doesn't report *actual* (i.e. compressed) bytes read...

Hi all. Love this website! My issue is as follows: I'm reading a zip file that's coming over a network from an HTTP "PUT" request. The request header tells me that the Content-Length is (say) 1Mb. The following code creates the ZipInputStream, and saves the zip contents to files in the current directory: ZipInputStream zis = new Zi...

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 convert characters into bytes. As of JDK 1.1, the preferred way to create a stream from ...

Java read file at a certain rate

Can someone point me to an article/algorithm on how can a read a long file at a certain rate? Say i do not want to pass 10 kb/sec while issuing reads. ...

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 same stream multiple times and the POIFSFileSystem closes the stream after use. What is the best way to cache the data from the input stream an...

HTTP inputstream class (C++)

I'm currently working on a project that does a lot of HTTP requests for data that it needs. It works by writing all the data into a buffer, then returning that buffer to the caller. It seems that waiting for the entire file to download data that can essentially streamed is a bad idea. Question 1: Is there already a library / public code...

Parsing a variable-length message

I am implementing the BitTorent protocol using Java via this spec. In the messages section all messages are fixed length except 2 of them; for one of them it's the only variable message after the handshake so I can check others and assume it's a piece message when no other messages met. But for the following message bitfield: <len=0001...

Repeat Unix Command in Java

Is there a way to periodically run a Unix command (ps in my case) in Java? The loop I wrote: while( this.check ) { try { ProcessBuilder pb = new ProcessBuilder("ps"); Process proc; System.out.println(" * * Running `ps` * * "); byte[] buffer; String input; ...

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

I need to know the size of an inputstream [Java]

My current situation is I have to read in a file and place it in an InputStream, and then also place the contents of the InputStreaminto a byte array which requires that i know the size of the InputStream. Any ideas? As requested, i will show the input stream that i am creating from an uploaded file InputStream uploadedStream = null; ...

java: datainputstream: do read calls take up processor time while waiting for data?

If I call read() on a DataInputStream, will it take up CPU cycles waiting for data or does it yield the current thread and get woken up by an interrupt signaling that data has arrived? My motivation for asking the question is to determine whether or not a stream reader needs to be in its own thread or not. A blocking read that takes up...

What is the optimal way for reading the contents of a webpage into a string in Java?

I have the following Java code to fetch the entire contents of an HTML page at a given URL. Can this be done in a more efficient way? Any improvements are welcome. public static String getHTML(final String url) throws IOException { if (url == null || url.length() == 0) { throw new IllegalArgumentException("url cannot be null or empty...

Most efficient way to create InputStream from OutputStream

This page: http://ostermiller.org/convert_java_outputstream_inputstream.html describes how to create an InputStream from OutputStream: new ByteArrayInputStream(out.toByteArray()) Other alternatives are to use PipedStreams and new threads which is cumbersome. I do not like the idea of copying many megabytes to new in memory Bytes a...

InputStream read doesn't read the data

I'm having an issue reading from a java input stream. I have a buffer of size 1024, and an input stream of size 29k-31k. I read the inputStream in a loop, but I only get 29 bytes for the first read, 39 for the second read, and nothing after that. The same behavior repeats for different InputStreams. (I'm writing the data to an output str...

IOException while reading from InputStream

I'm running into a strange problem while reading from an InputStream on the Android platform. I'm not sure if this is an Android specific issue, or something I'm doing wrong in general. The only thing that is Android specific is this call: InputStream is = getResources().openRawResource(R.raw.myfile); This returns an InputStream for...

Java: ignoring an input stream - will buffers overflow and bad things happen?

Hi all, I have a client connecting to my server. The client sends some messages to the server which I do not care about and do not want to waste time parsing its messages if I'm not going to be using them. All the i/o I'm using is simple java i/o, not nio. If I create the input stream and just never read from it, can that buffer fill...

InputStream or Reader wrapper for progress reporting

So, I'm feeding file data to an API that takes a Reader, and I'd like a way to report progress. It seems like it should be straightforward to write a FilterInputStream implementation that wraps the FileInputStream, keeps track of the number of bytes read vs. the total file size, and fires some event (or, calls some update() method) to r...