outputstream

Reading from a ZipInputStream into a ByteArrayOutputStream

I am trying to read a single file from a java.util.zip.ZipInputStream, and copy it into a java.io.ByteArrayOutputStream (so that I can then create a java.io.ByteArrayInputStream and hand that to a 3rd party library that will end up closing the stream, and I don't want my ZipInputStream getting closed). I'm probably missing something bas...

Testing what's written to a Java OutputStream

I am about to write junit tests for a XML parsing Java class that outputs directly to an OutputStream. For example xmlWriter.writeString("foo"); would produce something like <aTag>foo</aTag> to be written to the outputstream held inside the XmlWriter instance. The question is how to test this behaviour. One solution would of course be to...

How can I implement an OutputStream that I can rewind?

After writing out some processed content to an output stream, I need to revisit the beginning of the stream and write out some content metadata. The data I'm writing is very large, as much as 4Gb, and may be written either directly to a file or to an in-memory buffer, depending on various environmental factors. How can I implement an O...

Switching Writers on an OutputStream in Java

I have one method that opens a file and passes off to another function to write some data to it. That second method wants to use PrintWriter to write its data. However, I didn't want to require that everybody use PrintWriter to write to that stream. Currently it looks something like this ( sanitized example... don't bother criticizing m...

How do I zip on the fly and stream to Response.Output in real time?

I am trying to use the following code: I get a corrupted zip file. Why? The file names seem OK. Perhaps they are not relative names, and that's the problem? private void trySharpZipLib(ArrayList filesToInclude) { // Response header Response.Clear(); Response.ClearHeaders(); Response.Cache.SetCa...

Function that prints something to std::ostream and returns std::ostream?

I want to write a function that outputs something to a ostream that's passed in, and return the stream, like this: std::ostream& MyPrint(int val, std::ostream* out) { *out << val; return *out; } int main(int argc, char** argv){ std::cout << "Value: " << MyPrint(12, &std::cout) << std::endl; return 0; } It would be conveni...

java servlet: generate zip file from BLOBs

I'm trying to zip a large number of pdf files (stored as BLOBs in the DB) and then return the zip as an attachment to the user. What's the best way to do this without running into memory issues? Another note: I actually need to merge some PDFs prior to adding them to the ZipOutputStream. Therefore, a couple PDFs will need to be store...

Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter() ?

I couldn't find an authoritative answer to this with some Googling. In Java servlets, one can access the response body via response.getOutputStream() or response.getWriter(). Should one call .close() on this stream after it has been written to? On the one hand, there is the Blochian exhortation to always close output streams. On the oth...

Why Java OutputStream.write() Takes Integer but Writes Bytes

I am writing an OutputStream, just noticed this in the OutputStream interface, public abstract void write(int b) throws IOException; This call write one byte to the stream but why it takes integer as an argument? ...

Java - passing input into external C/C++ application

I'm trying to enter some value in external application using Java. Java application looks like this: Runtime runtime = Runtime.getRuntime(); // ... str build ... proc = runtime.exec(str); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())); bw.write(value); bw.flush(); bw.close(); if (proc.waitFor() ...

Sharing output streams through a JNI interface

I am writing a Java application that uses a C++ library through a JNI interface. The C++ library creates objects of type Foo, which are duly passed up through JNI to Java. Suppose the library has an output function void Foo::print(std::ostream &os) and I have a Java OutputStream out. How can I invoke Foo::print from Java so that...

Write a binary downloaded file to disk in Java

Hello, I have a software that allow to write add-on in javascript files (.js) that allow to use Java function (I don't know if this is common, I never saw java call in javascript file before) I need to download a binary file from a webserver and write it to the hard drive. I tried the following code: baseencoder = new org.apache.commo...

Multiple read from an InputStream at different rates

I'm about to load an online content (say an audio file). If I just open a connection to the remote file (for example by use of new URL().openStream()) and pass the remote InputStream to the audio player, it reads gradually from the network. If audio player library do not ask InputStream for more data, it reads nothing from the network, a...

Connecting an input stream to an outputstream

I saw some similar, but not-quite-what-i-need threads. I have a server, which will basically take input from a client, client A, and forward it, byte for byte, to another client, client B. I'd like to connect my inputstream of client A with my output stream of client B. Is that possible? What are ways to do that? Also, these clients...

Java Project Modules - use InputStream/OutputStream or .tmpFile/byte[]

Hi all, I found myself passing InputStream/OutputStream objects around my application modules. I'm wondering if it's better to - save the content to disk and pass something like a Resource between the various methods calls - use a byte[] array instead of having to deal with streams everytime. What's your approach in these situatio...

Reponse outputstream content length?

I am writing to output stream through various methods. How can I, before I close it, find out content length of the outputstream? ...

cancel a read from an InputStream

Currently I am using an ObjecInputStream to read from a Socket, and everything works fine using this simple code: ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); Object response = input.readObject(); Now I want to somehow cancel the read, without closing the stream, so that I can later read from it again. Is ...

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

It sounds like a very silly question but can anyone explain me about Input stream and output stream. I remain confused about when do we need input stream and when we need output stream? An explanation with some code snippet will be great. I just need to figure out their difference and usage. ...

Java Outputstream behavior when multiple outputstream objects are wrapped

Hi, I have a code that does compression, encryption and checksum on a File Outputstream. Following is the code- private void start() { OutputStream os = null; try { os = new FileOutputStream("/some/file"); os = wrapAllRequiredTransforms(os); //Write to os } finally { os.close(); } } private wrap...

How to implement a circular buffer in Blackberry or java me?

How can we program a circular buffer for Blackberry? ...