Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we read or use the contents of outputstream [closed]

Tags:

java

servlets

How can we read or use the contents of outputstream. In my case, I am using a method having signature.

public static OutputStream decryptAsStream(InputStream inputStream, String encryptionKey)

This method return OutputStream. I want get OutputStream to String. Is it possible to pipe the output from an java.io.OutputStream to a String in Java?

like image 853
Amit Singh Avatar asked Aug 03 '13 05:08

Amit Singh


People also ask

What is OutputStream close in java?

close() method closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened. The close method of OutputStream does nothing.

Do I need to close OutputStream?

copy(InputStream, OutputStream) must not close the OutputStream . You can use it for example to concatenate different InputStreams and in this case it would be a bad idea if it would close the provided Input- and/or OutputStream. As a rule of thumb any method should close only those steams it opens.

How does OutputStream write work?

Methods of OutputStream write() - writes the specified byte to the output stream. write(byte[] array) - writes the bytes from the specified array to the output stream. flush() - forces to write all data present in output stream to the destination. close() - closes the output stream.

How do you save an OutputStream file?

txt to FileOutputStream can be done as: FileOutputStream fout=new FileOutputStream(“file. txt”); Reading data from DataInputStream: The next step is to read data from DataInputStream and write it into FileOutputStream .


1 Answers

You can do something like following :

  OutputStream output = new OutputStream()
    {
        private StringBuilder string = new StringBuilder();
        @Override
        public void write(int x) throws IOException {
            this.string.append((char) x );
        }

        public String toString(){
            return this.string.toString();
        }
    };
like image 100
Ushani Avatar answered Oct 01 '22 18:10

Ushani