I have initialized an InputStream in a single method in a class and passing it to next method for processing. The InputStream essentially encapsulates CSV file for processing.
Another method calls 2 different methods passing in same InputStream one for retrieving headers and another for processing contents. The structure looks something as given below:
main() {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
processCSV(isr);
}
processCSV(Reader isr) {
fetchHeaders(isr);
processContentRows(isr);
}
fetchHeaders(Reader isr) {
//Use BufferedReader to retrieve first line of CSV
//Even tried mark() and reset() here
}
processContentRows(Reader isr) {
//Cannot read the values, fetches null from InputStream :(
}
Am I doing something wrong here? Is there any way I can reuse InputStream across different method calls.
I am putting up complete program that can mimic the issue below:
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class MarkResetTest
{
public static void main(String a[])
{
FileInputStream fis = null;
BufferedInputStream bis = null;
InputStreamReader isr = null;
BufferedReader br = null;
BufferedReader br2 = null;
try {
fis = new FileInputStream("C:/Test/Customers.csv");
bis = new BufferedInputStream(fis);
isr = new InputStreamReader(bis, "Unicode");
System.out.println("BR readLine()");
br = new BufferedReader(isr);
//System.out.println(br.markSupported());
br.mark(1000);
System.out.println(br.readLine());
br.reset();
//System.out.println(br.readLine());
System.out.println("BR2 readLine()");
br2 = new BufferedReader(isr);
System.out.println(br2.readLine());
}
catch(Exception e) {
System.out.println("Exception::" + e);
}
finally {
try {
br.close();
isr.close();
bis.close();
fis.close();
}
catch(Exception e) {
System.out.println("Exception while closing streams :: " + e);
}
}
}
}
The problem is in creating two BufferedReaders on top of the same Reader. When you read data from BufferedReader, it's likely to read more than the data it returns, into its buffer (hence the name). In other words, even though you've only read a single line from the BufferedReader, the InputStreamReader may have had a lot more data read from it - so if you read again from that InputStreamReader then you'll miss that data. The data has effectively been sucked from the InputStreamReader to the BufferedReader, so the only way of getting it out to client code is to read it from that BufferedReader.
In other words, your claim that:
Nope. fetchHeaders() only reads first line of CSV containing Headers.
is incorrect. It only uses that much data, but it reads more from the InputStreamReader.
As Ilya said, you should only create one BufferedReader on top of the original InputStreamReader, and pass that into both methods.
fetchHeaders can then use that BufferedReader to read a line, and processContentRows can do what it likes with the BufferedReader at that point - it's just a Reader as far as it needs to know.
So to modify Ilya's example slightly:
public static void main(String[] args) {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr);
processCSV(br);
}
private static void processCSV(BufferedReader reader) {
fetchHeaders(reader);
processContentRows(reader);
}
private static void fetchHeaders(BufferedReader reader) {
// Use reader.readLine() here directly... do *not* create
// another BufferedReader on top.
}
private static void processContentRows(Reader reader) {
// This could be declared to take a BufferedReader if you like,
// but it doesn't matter much.
}
You're not doing anything wrong. Just make sure that the method opening a stream/reader also closes it, in a finally block.
If you need a BufferedReader, I think you need to create it in in main method:
main() {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr);
processCSV(br);
}
processCSV(Reader isr) {
fetchHeaders(isr);
processContentRows(isr);
}
fetchHeaders(Reader isr) {
//Use BufferedReader to retrieve first line of CSV
//Even tried mark() and reset() here
}
processContentRows(Reader isr) {
//Cannot read the values, fetches null from InputStream :(
}
You can do this by using byte
public static void main(String[] args) throws IOException {
String str = "01 Lorem ipsum\n 02 dolor sit\n 03 amet\n";
InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) > -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
InputStream in1 = new ByteArrayInputStream(outputStream.toByteArray());
InputStream in2 = new ByteArrayInputStream(outputStream.toByteArray());
InputStreamReader isr1 = new InputStreamReader(in1);
BufferedReader br1 = new BufferedReader(isr1);
InputStreamReader isr2 = new InputStreamReader(in2);
BufferedReader br2 = new BufferedReader(isr2);
System.out.println("One line from br1:");
System.out.println(br1.readLine());
System.out.println();
System.out.println("One line from br2:");
System.out.println(br2.readLine());
System.out.println(br2.readLine());
System.out.println();
}
Related
I have a server that displays what the user asks for from the browser, I am using linux and when i run the server and ask for a file like Image.png using this link localhost:9999/Image.png on FireFox i get this message:
The image "localhost:9999/Image.png" cannot be displayed because it
contains errors.
But when i change the variable fileName to an HTML file it works perfectly and i can visualize the html page.
What am I doing wrong??
This is my server:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public static void main(String args[]) throws IOException {
// Declarem les variables a utilitzar
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inS = null;
OutputStream outS = null;
try
{
serverSocket = new ServerSocket(9999);
while(true)
{
socket= serverSocket.accept();
inS = socket.getInputStream();
outS = socket.getOutputStream();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(inS));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outS));
System.out.println("THis is what the user wants = " + br.readLine());
String fileName = "Image.png";
String extension= "";
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i+1);
}
String dataReturn = "";
if(extension.equals("png"))
{
bw.write("HTTP/1.0 200 OK\r\n");
bw.write("Content-Type: image/png\r\n");
bw.write("\r\n");
FileReader myFilepng = new FileReader(fileName);
Scanner scanner1 = new Scanner(myFilepng);
dataReturn = "";
while(scanner1.hasNextLine()) {
dataReturn = scanner1.nextLine();
System.out.println(dataReturn);
bw.write(dataReturn);
}
scanner1.close();
}else{
if(extension.equals("html"))
{
bw.write("HTTP/1.0 200 OK\r\n");
bw.write("Content-Type: text/html\r\n");
bw.write("\r\n");
bw.write("<TITLE>"+fileName+"/TITLE>");
FileReader myFile = new FileReader(fileName);
Scanner scanner = new Scanner(myFile);
dataReturn = "";
while(scanner.hasNextLine()) {
dataReturn = scanner.nextLine();
System.out.println(dataReturn);
bw.write(dataReturn);
}
scanner.close();
}
}
bw.close();
}catch(Exception e)
{
}
}
}catch (IOException e) {
System.out.println(e);
}
inS.close();
outS.close();
socket.close();
}
}
You are not writing the contents of your png file to your bw BufferedWriter. Instead you are only sending the header of the response to the client. As you are indicating your response is a png image and there is no data, your browser is telling you the image contains errors (in fact, it does not contains nothing at all).
Open the png filename, write the data to your "bw" buffer to send it to the client. That should be enough.
Edit:
To to that, try the following code for your "if" is image:
if(extension.equals("png"))
{
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
DataOutputStream binaryOut = new DataOutputStream(outS);
binaryOut.writeBytes("HTTP/1.0 200 OK\r\n");
binaryOut.writeBytes("Content-Type: image/png\r\n");
binaryOut.writeBytes("Content-Length: " + data.length);
binaryOut.writeBytes("\r\n\r\n");
binaryOut.write(data);
binaryOut.close();
}
Note the use of a binary stream in comparison to the text stream you use in case of html.
This is my test program. I need it to apply somewhere.This may be small, sorry for that. But I'm a starter still. So kindly help me.
try{
File file1 = new File("c:\\Users\\prasad\\Desktop\\bugatti.jpg");
File file2 = new File("c:\\Users\\prasad\\Desktop\\hello.jpg");
file2.createNewFile();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
String data = null;
StringBuilder imageBuild = new StringBuilder();
while((data = reader.readLine())!=null){
imageBuild.append(data);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new PrintWriter(new FileOutputStream(file2)));
writer.write(imageBuild.toString());
writer.close();
}catch(IOException e){
e.printStackTrace();
}
This is file1
and This is file2
You can do either of these two:
private static void copyFile(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
or maybe this if you want to use streams:
private static void copyFile(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}
Images do not contain lines or even characters. You therefore should not be using readLine() or even Readers or Writers. You should rewrite the copy loop using input and output streams directly.
is there a reason Eclipse gives me the following resource leak warning: Resource leak: 'br' is never closed" ? The code I am talking about is at the bottom of this post.
I thought my finally block had it all covered, my reasoning:
res will only be null if the FileInputStream constructor threw and therefore nothing has to be closed
res will be the inputstream if the InputStreamReader constructor throws (malformed encoding string for example) and then only the InputStream must be closed so ok
etc...
So what am I missing? Or could this be an eclipse bug?
Kind regards!
S.
public static String fileToString(String fileName, String encoding) throws IOException {
InputStream is;
InputStreamReader isr;
BufferedReader br;
Closeable res = null;
try {
is = new FileInputStream(fileName);
res = is;
isr = new InputStreamReader(is, encoding);
res = isr;
br = new BufferedReader(isr);
res = br;
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line);
builder.append(LS);
}
return builder.toString();
} finally {
if (res != null) {
res.close();
}
}
}
Eclipse probably just isn't understanding the shuffling you're doing with the res variable.
I recommend using the try-with-resources statement (available in Java 7 and up, so three and a half years now), it dramatically simplifies these sorts of chains:
public static String fileToString(String fileName, String encoding) throws IOException {
try (
InputStream is = new FileInputStream(fileName);
InputStreamReader isr = new InputStreamReader(is, encoding);
BufferedReader br = new BufferedReader(isr)
) {
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line);
builder.append(LS);
}
return builder.toString();
}
}
If you can't use try-with-resources, you probably want something like the Apache Commons IOUtils class's closeQuietly methods (either literally that one, or your own) rather than shuffling res around, which is awkward to read and I daresay prone to maintenance issues.
Using IOUtils might look like this:
public static String fileToString(String fileName, String encoding) throws IOException {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
is = new FileInputStream(fileName);
isr = new InputStreamReader(is, encoding);
br = new BufferedReader(isr)
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line);
builder.append(LS);
}
br.close();
return builder.toString();
}
finally {
IOUtils.closeQuietly(br, isr, is);
}
}
Note how I use a normal close in the try, but then ensure cleanup in the finally.
But try-with-resources is the better answer, as it's more concise and hooks into the new(ish) "suppressed exceptions" stuff.
Side note: There's no reason for the = null initialization of line, you assign it on the next line.
Side note 2: If the file is likely to be of any size, consider finding out how big it is in advance and setting the capacity of the StringBuilder in the constructor. StringBuilder's default capacity is 16, so a file of even a few hundred bytes involves several reallocations of StringBuilder's internal buffer.
I have the following code, but the char[] cc does not seem to get written when I checked the output file. Can someone tell me what's wrong?
import java.io.*;
class Test {
public static void main(String[] args) {
System.out.printf("start of main\n");
char[] cc = new char[300];
try {
String s = "this is a test.";
System.arraycopy(s.toCharArray(), 0, cc, 0, s.length());
System.out.printf("cc = %s\n", new String(cc));
String filename = "tst.data";
DataOutputStream ostream = new DataOutputStream(new FileOutputStream(filename));
OutputStreamWriter writer = new OutputStreamWriter(ostream);
writer.write(cc, 0, 300);
ostream.close();
DataInputStream istream = new DataInputStream(new FileInputStream(filename));
InputStreamReader reader = new InputStreamReader(istream);
char[] newcc = new char[300];
reader.read(newcc, 0, 300);
istream.close();
System.out.printf("newcc = %s\n", new String(newcc));
} catch (Exception e) {
System.out.printf("Exception - %s\n", e);
}
}
}
You need to close the outermost I/O wrapper.
Replace
ostream.close();
by
writer.close();
Unrelated to the concrete problem, those DataOutputStream and DataInputStream wrappers are unnecessary in this context. Remove them. Finally, you should be closing the streams in a finally block. See also this related question: Do I have to close FileOutputStream which is wrapped by PrintStream?
It seams that writer still not flush buffer to ostream.
You can use writer.flush();
like this
writer.write(cc, 0, 300);
writer.flush();
Hi i am using the following code for uploding my file from android phone to the server bt the file does not upload completely..e.g i uploded a 11kb file and got only 8kb file at the server.What am i doing wrong?
Client side
Socket skt = new Socket"112.***.*.**", 3000);
String FileName=fil.getName();
PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())),true);
out2.println("Upload");
out2.println(FileName);
out2.println(spinindx);
out2.println(singleton.arrylst_setngs.get(0).toString());
out2.println(singleton.arrylst_setngs.get(1).toString());
out2.println(singleton.arrylst_setngs.get(2).toString());
out2.println(singleton.arrylst_setngs.get(3).toString());
out2.println(singleton.arrylst_setngs.get(4).toString());
out2.flush();
//Create a file input stream and a buffered input stream.
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
out.write(buf,0,i);
publishProgress(in.available());
System.out.println(i);
}
//Close the writers,readers and the socket.
in.close();
out.flush();
out.close();
out2.close();
skt.close();
}
catch( Exception e ) {
System.out.println(e);
}
The server side
InputStream inStream = socket.getInputStream();
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
String Request=inm.readLine();
if(Request.equals("Upload")){
fileName = inm.readLine();
chosn = inm.readLine();
lt=inm.readLine();
cs = inm.readLine();
om = inm.readLine();
o = inm.readLine();
check=inm.readLine();
//Read, and write the file to the socket
BufferedInputStream in = new BufferedInputStream(inStream);
int i=0;
File f=new File("D:/data/"+filePrefx+fileName);
if(!f.exists()){
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream("D:/data/"+filePrefx+fileName);
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
System.out.println(i);
out.write(buf,0,i);
System.out.println("Receiving data...");
}
in.close();
inStream.close();
out.close();
fos.close();
socket.close();
Looks like you are using both a BufferedReader and a BufferedInputStream on the same underlying socket at the server side, and two kinds of output stream/writer at the client. So your BufferedReader is buffering, which is what it's supposed to do, and thus 'stealing' some of the data you're expecting to read with the BufferedInputStream. Moral: you can't do that. Use DataInputStream & DataOutputStream only, and writeUTF()/readUTF() for the 8 lines you are reading from the client before the file.
You shared the same underlying InputStream between your BufferedReader and bufferedInputStream.
What happened is, when you do the reading through BufferedReader, it reads more than the a few lines you requested from the underlying InputStream into its own internal buffer. And when you create the BufferedInputStream, the data has already been read by the BufferedReader. So Apart from what EJP suggested not to use any buffered class, you can create the BufferedInputStream, and then create the Reader on Top of it. The code is something like this:
BufferedInputStream in = new BufferedInputStream(inStream);
Reader inm = new InputStreamReader(in);
Add it to the beginning of your server code and remove this line:
BufferedInputStream in = new BufferedInputStream(inStream);
See this, i never tried though
void read() throws IOException {
log("Reading from file.");
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine() + NL);
}
}
finally{
scanner.close();
}
log("Text read in: " + text);
}
Shamelessly copied from
http://www.javapractices.com/topic/TopicAction.do?Id=42