Is BufferedInputStream and BufferedOutputstream different from InputStream and Outputstream - java

recently i had troubles working with InputStreams and OutputStreams when i was trying to implement a basic file downloader in my android application.. to elaborate things this is how i did it..
i get an InputStream object using the apache HttpClient classes then tried writing the stream to a file.. but strangely when i buffer the InputStream or the OutputStream i get an unreadable file.... this is the code..
//to make the code concise i removed exceptions and stream closing..
private void download(InputStream in,String fileName){
//if i dont use the buffered thing and read directly from in everything is ok
// same is the buffered out i had to use in/outstream
BufferedInputStream bufferedIn = new BufferedInputStream(in);
FileOutputStream fout = new FileOutputStream(new File(fileName));
BufferedOutputstream bufferedOut = new BufferedOutputstream(fout);
int read = -1;
while((read = bufferedIn.read()) != -1){
bufferedOut.write(read);
}
//close the buffers
}

You have to flush the buffered outputstream when you're done with it.

In any case you probably want to flush() your output (done implicitly by close()), but with BufferedOutputStream this is even more important than with a other OutputStreams. If you have a FileOutputStream, the only buffering performed is that of the OS. If you have a BufferedOutputStream, Java performs its own buffering on top of it.
If you use Java 7 or newer, I'd recommend to write the code like this:
try (BufferedInputStream bIn = new BufferedInputStream(in);
BufferedOutputStream bOut = new BufferedOutputStream(new FileOutputStream(fileName))) {
for (int read; ((read = bIn.read()) != -1; )
bOut.write(read);
}
In your case I suspect you were closing the FileOutputStream but not the BufferedOutputStream. Therefore the file was truncated or even empty because the data buffered in the BufferedOutputStream was not flushed.

Related

downloaded file supposed to be .png

i dont know whats wrong with my code, i try to download png from server with sambautil
if (!path.equals("")) {
SambaUtil su = new SambaUtil();
byte[] data = su.openfile(path);
log.info(path);
ByteArrayInputStream inStream = new ByteArrayInputStream(su.openfile(path));
PrintWriter outStream1 = resp.getWriter();
int bytes;
while ((bytes = inStream.read()) != -1) {
outStream1.write(bytes);
}
inStream.close();
outStream1.close();
}
the connection is right, but output data seems wrong
the output
Any idea?
That is because you are taking the bytes that make up the .PNG file and printing it out instead of saving it as a file which is what I think you want to do.
Instead of
PrintWriter outStream1 = resp.getWriter();
Replace with this
OutputStream outStream1 = new FileOutputStream("somefile.png");
Then open the 'somefile.png' and you should be able to see the image that you downloaded via Samba

Strange behavior of java streaming

I've the necessity to share a streaming of data between two instances as below:
// get EClasses which should be connected
final uk.man.xman.xcore.Parameter source = getParameter(sourceAnchor);
final uk.man.xman.xcore.Parameter target = getParameter(targetAnchor);
// Set data channels
//Output stream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream));
source.setOutputStream(dataOutputStream);
//Input stream
DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
target.setInputStream(inpuDataStream);
Everything works ok if I write, during those lines of code. Strangely, when I need to use the data channel to write something in another class, like here:
DataOutputStream dataOutputStream = (DataOutputStream) inputParameter.getOutputStream();
System.out.println("WRITE:" + attributes.getValue("value"));
dataOutputStream.writeUTF(attributes.getValue("value"));
dataOutputStream.flush();
I am not able to read, and I really do not know why. Am I missing something?
Thanks for your time
Not sure if that's what you're asking, but you're creating an InputStream that reads from an empty byte array. That doesn't make much sense:
// create an Output stream that will write in memory
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
...
// transform what has been written to the output stream into a byte array.
// Since othing has been written yet, outputStream.toByteArray() returns
// an empty array
DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));

what about one BufferedOutputStream wrap another BufferedOutputStream?

what about one BufferedOutputStream wrap another BufferedOutputStream? this question is simple. but confused.
As the following code,
OutputStream file = new FileOutputStream("test.txt");
OutputStream buffer = new BufferedOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(buffer); //wrap buffer twice
OutputStream outputStream = new ObjectOutputStream(bufferedOutputStream); // wrap as ObjectOutputStream
BufferedOutputStream bufferedOutputStream1 = new BufferedOutputStream(outputStream); //wrap back as BufferedOutputStream
ObjectOutput output = new ObjectOutputStream(bufferedOutputStream1);
What about it? Question?
If you're asking whether an inefficiency is introduced, the answer is 'no'. The code is optimised to handle that case, or rather the case where the transfer size >= the buffer size.

Reading data in Java

What's the difference between using a BufferedReader and a BufferedInputStream?
A BufferedReader is used for reading character data. A BufferedOutputStream is used for writing binary data.
Any classes inheriting from Reader or Writer deal with 16-bit unicode character data, whereas classes inherting from InputStream or OutputStream are concerned with processing binary data. The classes InputStreamReader and OutputStreamWriter can be used to bridge between the two classes of data.
Bufferedreader reads data from a file as a string. BufferedOutputStream writes to a file in bytes. BufferedInputStream reads data in bytes
Sample to Bufferedreader:
try {
BufferedReader br = new BufferedReader(new FileReader(new File(your_file));
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);
}
}
Sample to BufferedOutputStream:
//Construct the BufferedOutputStream object
bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));
//Start writing to the output stream
bufferedOutput.write("Line 1".getBytes());
bufferedOutput.write("\r\n".getBytes());
bufferedOutput.write("Line 2".getBytes());
bufferedOutput.write("\r\n".getBytes());
Bufferedinputstream reads in byte:
Sample
:
//Construct the BufferedInputStream object
bufferedInput = new BufferedInputStream(new FileInputStream(filename));
int bytesRead = 0;
while ((bytesRead = bufferedInput.read(buffer)) != -1) {
String chunk = new String(buffer, 0, bytesRead);
System.out.print(chunk);
}
As the names imply, one is for reading data, and the other is for outputting data.

Write a binary downloaded file to disk in Java

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.commons.codec.binary.Base64();
url = new java.net.URL("https://server/file.tgz");
urlConnect = url.openConnection();
urlConnect.setDoInput(true);
urlConnect.setDoOutput(true);
urlConnect.setRequestProperty("authorization","Basic "+ java.lang.String(baseencoder.encodeBase64(java.lang.String( username + ":" + password ).getBytes())));
urlConnect.setRequestProperty("content-type","application/x-www-form-urlencoded");
is = new java.io.DataInputStream(urlConnect.getInputStream());
fstream = new FileWriter("C:\\tmp\\test.tgz");
out = new BufferedWriter(fstream);
while((data = is.read()) != -1){
out.write(data);
}
out.close();
is.close();
The resulting file is no longer a valid gzip archive. I'm sorry if I did a huge error but I'm not a programmer and don't know Java too much.
Don't use a FileWriter - that's trying to convert the data into text.
Just use FileOutputStream.
byte[] buffer = new byte[8 * 1024];
InputStream input = urlConnect.getInputStream();
try {
OutputStream output = new FileOutputStream(filename);
try {
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
I know this question is already answered, but a simpler approach is to use Apache Commons IO's IOUtils.copy() method, which can fully copy an InputStream to an OutputStream.
DataInputStream is meant for reading Java primitives, not for generic data.
It's also redundant, as urlConnect.getInputStream(); already returns an InputStream, and all InputStreams support read().
is = urlConnect.getInputStream();
P.S. This is assuming is and bis are the same variable. Otherwise, you're reading the wrong stream in the loop.
Just read about LimitInputStream sounds like it does exactly what you are doing, buffering the input stream for greater efficiency.
You can even use NIO FileChannel#transferFrom method.
URL website = new URL(urlToDownload);
try (ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(filePath);) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
Reference link1, link2

Categories