I'm trying to write a simplest java web server program following an instruction which is only able to handle GET inquiry. The main idea is to get an ObjectOutputStream from a socket, use an ObjectInputStream to open a local file and write it into the ObjectOutputStream byte by byte.
The serve() is attached below. It takes an ObjectOutputStream I want to write to and the path to a file as parameters.
public void serve(ObjectOutputStream out, String path) throws IOException {
System.out.println("Trying to serve: " + path);
File file = new File(path);
if (!file.exists()) {
//return an HTTP 404
} else {
out.writeBytes("HTTP/1.1 200 OK\n\n");
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(file));
int data;
while ((data = in.readByte()) != -1) {
out.writeByte((byte) data);
}
System.out.println("Request valid.");
} catch (IOException e) {
System.out.println("Error in serve(): sending file: " + e.getMessage());
} finally {
if (null != in)
in.close();
}
}
}
However, when I use browser to access localhost:8080 (the port is at 8080), it throws an IOException
invalid stream header: 3C68746D
I believe it's in out.writeByte((byte) data); step. Can you tell me why and how to fix it? Thanks ahead.
ObjectInputStream and ObjectOutputStream are used for object serialization in java.
Please refer the below article to understand the usage of these streams.
http://java.sun.com/developer/technicalArticles/Programming/serialization/
For your code, you could better use BufferedInputStream and BufferedOutputStream wherever you find corresponding Object Stream.
Related
I have a method that writes data from a list to a file, a method that reads data from a file into a list and a method that writes data from a list in a file to the specified number of times. I'm trying to extract data from a file after I use the first method writeFile () everything works fine. I read the data from the file into the list by readFile () method. After that I use my method which writes to the file the number of times I need, everything is fine, it writes multyWrite (). But after that I can not read the data from the file in the readFile () method since I get `
Exception stack trace:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1599)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at ProductService.readFile(ProductService.java:47)
at Main.main(Main.java:21)
I know that I should use objectOutputStream.reset (), but where would it be better to use it?
private String fileName;
private ProductInterface<FlyingMachine> productService = new ProductInterfaceImpl();
private ObjectOutputStream objectOutputStream;
private FileOutputStream fileOutputStream;
public ProductService(String fileName) throws IOException {
this.fileName = fileName;
fileOutputStream = new FileOutputStream(fileName);
this.objectOutputStream = new ObjectOutputStream(fileOutputStream);
}
public void writeFile() throws IOException {
try {
for (FlyingMachine f : productService.getProductContainer()) {
objectOutputStream.writeObject(f);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
}
}
}`
public void readFile() throws IOException {
ObjectInputStream objectInputStream = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fileName);
objectInputStream = new ObjectInputStream(fileInputStream);
while (fileInputStream.available() > 0) {
FlyingMachine flyingMachine = (FlyingMachine) objectInputStream.readObject();
productService.getProductContainer().add(flyingMachine);
}
} catch (ClassNotFoundException | EOFException e) {
e.printStackTrace();
} finally {
if (objectInputStream != null) {
objectInputStream.close();
fileInputStream.close();
}
}
}
public void multyWrite(int number) throws IOException {
for (int i = 0; i < number; i++) {
try {
fileOutputStream = new FileOutputStream(fileName, true);
objectOutputStream = new ObjectOutputStream(fileOutputStream);
for (FlyingMachine f : productService.getProductContainer()) {
objectOutputStream.writeObject(f);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
objectOutputStream.flush();
objectOutputStream.close();
}
}
}
}
You create a new ObjectOutputStream in the constructor. In writeFile you use that OOS instance and close it. But in multyWrite you don't use it and instead create new instances.
Now when you call multyWrite without having called writeFile first, that first OOS will still be open, but the OOS you create in multyWrite doesn't know that - thus causing your file to have two OOS headers after another.
And then when you try to read such a file, the ObjectInputStream will find the first header (all is fine) and then unexpectedly find the second header, while it expected a type code. That header starts with 0xAC, hence throwing the exception message "invalid type code: AC".
To fix this, either have multyWrite use the OOS constructed in your constructor, the same way writeFile does, or make sure that that OOS is closed before you create a new one.
It's generally not a good idea to open a stream (of any kind) in a constructor and then rely on external code calling a specific method to close it. Better create streams when you need them and close them directly.
I am making an android application that should send and receive some json files through a wifi direct connection (whenever another device is connected, they both trade all their json files).
This question is mostly about what would be the best practice, since I am fairly new both to android and java.
When a wifi direct connection is established one of the two devices (the group owner) becomes the server and opens a server socket; the other connects to said socket and sends one single json file to the server.
I want the client to send all his json files and then receive all the json files from the server, and I'm wondering how it should be done: how do I tell one file is over? Should I send the lenght of the file ahead, or make the client wait for an acknowledgement for when the server is done reading?
Can I signal "End of Data" (by closing the OutputStream?) to stop the reading loop on the receiving end and then start sending another file?
To have some context, this is currently the code I'm using:
Client side
#Override
protected String doInBackground(Void... params) {
Socket socket = new Socket();
try {
Log.d(TAG, "Opening client socket - ");
socket.bind(null);
socket.connect((new InetSocketAddress(mHost, Constants.PORT)), SOCKET_TIMEOUT);
Log.d(TAG, "Client socket - " + socket.isConnected());
OutputStream stream = socket.getOutputStream();
//There is only one file, so the loop here runs only once
File dir = Utils.graffitiDir(context);
for (File tmp : dir.listFiles()) {
FileInputStream in = new FileInputStream(tmp);
Utils.copyFileInOut(in, stream);
}
Log.d(TAG, "Client: Data written");
return "ok";
} catch (IOException e) {
Log.e(TAG, e.getMessage());
return null;
}
finally {
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
// Give up
e.printStackTrace();
}
}
}
}
}
Server side
#Override
protected String doInBackground(Void... params) {
try {
ServerSocket serverSocket = new ServerSocket(Constants.PORT);
byte buf[] = new byte[4];
String res;
Log.d(TAG, "Server: Socket opened");
Socket client = serverSocket.accept();
Log.d(TAG, "Server: connection done");
File f = new File(context.getFilesDir(), Constants.DIRECTORY);
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
f.createNewFile();
File newf = new File(f, Calendar.getInstance().getTime().toString());
Log.d(TAG, "server: copying files " + f.toString());
InputStream inputstream = client.getInputStream();
//copyFile(inputstream);
Utils.copyFileInOut(inputstream, new FileOutputStream(newf));
serverSocket.close();
return newf.getAbsolutePath();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
return null;
}
}
The copyFileInOut function:
public static boolean copyFileInOut(InputStream inputStream, OutputStream out) {
byte buf[] = new byte[1024];
int len;
long startTime=System.currentTimeMillis();
try {
while ((len = inputStream.read(buf)) != -1) {
out.write(buf, 0, len);
Log.d("copyfile", "I'm writing");
}
out.close();
inputStream.close();
long endTime=System.currentTimeMillis()-startTime;
Log.d("copyfile", "Time taken to transfer all bytes is : " + endTime);
} catch (IOException e) {
Log.d(TAG, e.toString());
return false;
}
return true;
}
As a sidenote, I've seen a couple of similar questions (Sending and receiving files on socket) where the answer suggested to send the length of the file ahead, but I feel like my situation is a bit different, and I don't have the necessary experience to find out what is the best solution. I apologize if this is an obvious question, but I couldn't find an answer by myself.
In your case you should
1. send the number of files you are going to send first,
2. then send length of a file and that file. repeat for all files.
after this the server can also use the same order of sending number of files, the size of a file, that file repeat for all files.
My data is not getting transferred to the output file , I always get an Exception.
import java.io.*;
import java.util.*;
class TransferData {
public static void main(String[] args) {
String path1="E:\\IO\\Input.txt";
String path2="E:\\IO\\Output.txt";
int data;
System.out.println("Transfering started...");
try {
FileInputStream fis=new FileInputStream(path1);
FileOutputStream fos=new FileOutputStream(path2);
while((data=fis.read())!=-1) {
fos.write(data);
}
}
catch(Exception e) {
System.out.println("exception caught!");
}
System.out.println("Completed...");
}
}
How do I transfer data to output file ?
Tested this code on my local machine it is works without exceptions.
Check is file E:/IO/Input.txt exists.
IS Directory E:/IO is writeable for your user
(If file E:/IO/Output.txt already exists check is it writeable and not opened in another programm)
By code:
It is good practice to close FIS and FOS after programm finished execution.
public class TransferData {
public static void main(String[] args) {
String path1 = "E:\\IO\\Input.txt";
String path2 = "E:\\IO\\Output.txt";
int data;
System.out.println("Transfering started...");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(path1);
fos = new FileOutputStream(path2);
while ((data = fis.read()) != -1) {
fos.write(data);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Completed...");
}
}
If you replace System.out.println("exception caught!"); with e.printStackTrace(); then you will get a much more useful error message.
If you then post that error message here, people will be able to help you much more easily.
It could be the case that the program cannot find the file you're trying to read.
I highly suggest to use e.printStackTrace() as the others suggested.
One possible problem might be the filesystem permissions or the file you are trying to read from being not existent.
You might also want to use a "try with resources" to simplify your code.
Your code is missing a close statement for your Streams too.
All together your code would look something like this:
import java.io.*;
import java.util.*;
class TransferData {
public static void main(String[] args) {
String path1="E:\\IO\\Input.txt";
String path2="E:\\IO\\Output.txt";
int data;
System.out.println("Transfering started...");
try (
FileInputStream fis=new FileInputStream(path1);
FileOutputStream fos=new FileOutputStream(path2)
) {
while((data=fis.read())!=-1) {
fos.write(data);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
One last thing, if you post your code on StackOverflow, please do not mix different formatting styles (e.g. { in the same line as an if and sometimes in the next) and try to have the code well formatted from the beginning.
Add e.printStackTrace() to your catch block, and post the data printed in your console here, people will be able to help you better.
The most likely cause of the exception getting thrown is that the system is not able to find the file "E:\\IO\\Input.txt" or "E:\\IO\\Output.txt" make sure that the file's are there and Output.txt is not set to read only.
Lets assume I have an com.liferay.portal.kernel.repository.model.FileEntry instance fileEntry that I'd like to copy.
I can achieve this using
com.liferay.portlet.documentlibrary.service.DLAppServiceUtil.addFileEntry(...,java.io.InputStream is,...)
In this case, I get InputStream from my FileEntry using fileEntry.getContentStream().
FileEntry fileEntry;
InputStream inputStream = fileEntry.getContentStream();
FileEntry fileEntry2 = DLAppServiceUtil.addFileEntry(repositoryId, folder.getFolderId(), fileName, mimeType, fileName, "file was created in " + new Date(), fileName + " updated", inputStream, size, serviceContext);
Question : Do I need to perform a close on that InputStream ?
StreamUtil.cleanUp(inputStream);
where StreamUtil.cleanUp:
public static void cleanUp(InputStream inputStream) {
try {
if (inputStream != null) {
inputStream.close();
}
}
catch (Exception e) {
if (_log.isWarnEnabled()) {
_log.warn(e, e);
}
}
}
You should but you do not have to.
Older versions of Java required that you close your InputStream. But in all JAVA versions that Liferay supports, the streams are automatically closed once all there are no more references. However, you are at the mercy of the garbage collector and its schedule.
I have a flow which I have just converted from synchronous into queued-asynchronous.
At some point in a foreach, I am opening a file and setting a FileInputStream, as follows:
public class FileAsStream {
// return a fileInputStream.
public FileInputStream fileAsStream(String fileName) {
File file = new File(fileName);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (IOException e) {
e.printStackTrace();
} finally {
return fis;
}
}
The FileInputStream then becomes my payload, and I'm sending it off to http or sftp endpoints. When the flow was synchronous, I could then run #[payload.close()] and close the stream. But now that it is asynchronous it doesn't look like I can. It fails when I attempt to close the stream. My question is whether it matters if I close the stream or not. Does Mule wrap up objects created in the context of the flow? Or do I need to somehow close the stream after it has been sent to the endpoint?