objectoutputstream

Using resources of try-with-resources outside try

时光怂恿深爱的人放手 提交于 2021-01-28 03:50:40
问题 I'm using SocketChannel to send message between a server and a client. Once a client connects with a server, the server opens the InputStreams and OutputStream in a try-with-resources try, to receive messages from the client and send messages to the client, like so: try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {} I want to access out outside of the try. The try contains a while loop that

Using resources of try-with-resources outside try

好久不见. 提交于 2021-01-28 02:44:07
问题 I'm using SocketChannel to send message between a server and a client. Once a client connects with a server, the server opens the InputStreams and OutputStream in a try-with-resources try, to receive messages from the client and send messages to the client, like so: try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {} I want to access out outside of the try. The try contains a while loop that

How to read from file to Set with objectInputStream

我与影子孤独终老i 提交于 2020-06-22 04:31:41
问题 I'm trying to read some information from a file with ObjectInputStream method. For some reason, the file does not contain the information after the command executed. No error appears. This is my code : public boolean findUser(String user_id) throws Exception { try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fILENAME))){ //System.out.println("Adsfasdf"); System.out.println(objectInputStream); Set<MarkoliaUser> users = (Set<MarkoliaUser>) objectInputStream

Java ObjectOutputStream and updating a file

一曲冷凌霜 提交于 2020-01-30 12:31:37
问题 I am having trouble figuring out one implementation problem, I have one class, it behaves like list but instead of holding a file in some collection it saves them on a disk. The problem occurs when I want to add some element to my list. At the start of my file I have one int that tells me how many objects there are in my list, but I can't figure out elegant way to update this value. I have something like this: public boolean add(T element) { try { out.writeObject(element); out.flush(); //and

Java ObjectOutputStream and updating a file

荒凉一梦 提交于 2020-01-30 12:29:59
问题 I am having trouble figuring out one implementation problem, I have one class, it behaves like list but instead of holding a file in some collection it saves them on a disk. The problem occurs when I want to add some element to my list. At the start of my file I have one int that tells me how many objects there are in my list, but I can't figure out elegant way to update this value. I have something like this: public boolean add(T element) { try { out.writeObject(element); out.flush(); //and

Performance issue using Javas Object streams with Sockets

守給你的承諾、 提交于 2020-01-20 05:46:48
问题 I'm trying to do local IPC using Sockets and Object streams in Java however I'm seeing poor performance. I am testing the ping time of sending an object via an ObjectOutputStream to receiving a reply via an ObjectInputStream over a Socket. Here's the Requestor: public SocketTest(){ int iterations = 100; try { Socket socket = new Socket("localhost", 1212); ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream objectOutputStream = new

ObjectOutputStream and java.io.StreamCorruptedException

我是研究僧i 提交于 2020-01-14 14:50:29
问题 When I try to send a customobject (See Content.java) from my client to the server with ObjectOutputStream, I get StreamCorruptedException after the first object is sent. So if I try to send another object I get the exception, (it works the first time). I have googled and read a LOT of stuff, and now I'm about to give up, so I ask for your help. Client.java public class Client extends Thread { private final static String TAG ="Client"; private final static String IP = "10.0.2.2"; private final

ObjectOutputStream and java.io.StreamCorruptedException

有些话、适合烂在心里 提交于 2020-01-14 14:50:25
问题 When I try to send a customobject (See Content.java) from my client to the server with ObjectOutputStream, I get StreamCorruptedException after the first object is sent. So if I try to send another object I get the exception, (it works the first time). I have googled and read a LOT of stuff, and now I'm about to give up, so I ask for your help. Client.java public class Client extends Thread { private final static String TAG ="Client"; private final static String IP = "10.0.2.2"; private final

Java - ClassNotFound Exception when working with ObjectInputStream and ArrayList

巧了我就是萌 提交于 2020-01-05 12:10:13
问题 I'm trying to move the contents of an ArrayList from the client to the server. When trying to readObject() the input I receive a ClassNotFoundException. Here is my code, along with the stack trace: Client: ArrayList<Person> people = new ArrayList<Person>(); ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream()); for (Person p : people) { output.writeObject(p); } Server: ArrayList<Employee> employees = new ArrayList<Employee>(); ObjectInputStream input = new

Sending Java object of an unknown class

此生再无相见时 提交于 2020-01-04 13:57:47
问题 searching for a long time, but didn't find any answer on that one: I have a server and I have a client. The server should receive an object via ObjectInputStream and ObjectOutputStream . That already works for any class , known on the server. Now I want to send an Object of a class the server doesn't know. He only knows the interface of that class. And that obviously fails... How can I avoid the ClassNotFoundException ? I thought interfaces were the solution. I only want to access the