How do I convert an OutputStream to an InputStream?

There seem to be many links and other such stuff, but no actual code using pipes. The advantage of using java.io.PipedInputStream and java.io.PipedOutputStream is that there is no additional consumption of memory. ByteArrayOutputStream.toByteArray() returns a copy of the original buffer, so that means that whatever you have in memory, you now have two copies of … Read more

Preferred way to use Java ZipOutputStream and BufferedOutputStream

You should always wrap the BufferedOutputStream with the ZipOutputStream, never the other way around. See the below code: FileOutputStream fos = new FileOutputStream(“hello-world.zip”); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); try { for (int i = 0; i < 10; i++) { // not available on BufferedOutputStream zos.putNextEntry(new ZipEntry(“hello-world.” + i + … Read more

Connecting an input stream to an outputstream

Just because you use a buffer doesn’t mean the stream has to fill that buffer. In other words, this should be okay: public static void copyStream(InputStream input, OutputStream output) throws IOException { byte[] buffer = new byte[1024]; // Adjust if you want int bytesRead; while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } … Read more

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

Normally you should not close the stream. The servlet container will automatically close the stream after the servlet is finished running as part of the servlet request life-cycle. For instance, if you closed the stream it would not be available if you implemented a Filter. Having said all that, if you do close it nothing … Read more

Is it necessary to close each nested OutputStream and Writer separately?

Assuming all the streams get created okay, yes, just closing bw is fine with those stream implementations; but that’s a big assumption. I’d use try-with-resources (tutorial) so that any issues constructing the subsequent streams that throw exceptions don’t leave the previous streams hanging, and so you don’t have to rely on the stream implementation having … Read more

Byte[] to InputStream or OutputStream

You create and use byte array I/O streams as follows: byte[] source = …; ByteArrayInputStream bis = new ByteArrayInputStream(source); // read bytes from bis … ByteArrayOutputStream bos = new ByteArrayOutputStream(); // write bytes to bos … byte[] sink = bos.toByteArray(); Assuming that you are using a JDBC driver that implements the standard JDBC Blob interface … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)