I am currently working on a UDP server for a game. In this server use a ByteArrayInputStream
and a ObjectInputStream
every tick to convert serialized bytes to objects. Is it more efficient to create one variable for the streams and close them once when the program is closing?
like so:
class Main {
private static ByteArrayInputStream byteIn;
private static ObjectInputStream objectIn;
public static void main(String[] args) {
while(true){
receive();
}
//when program is done call close();
}
public static void receive(){
byteIn = new ByteArrayInputStream();
objectIn = new ObjectInputStream(new BufferedInputStream(byteIn));
//do something
}
public static void close(){
objectIn.close();
byteIn.close();
}
}
Or is it more efficient create and close new streams every time?
like so:
class Main {
public static void main(String[] args) {
while(true){
receive();
}
}
public static void receive(){
ByteArrayInputStream byteIn = new ByteArrayInputStream();
ObjectInputStream objectIn = new ObjectInputStream(new BufferedInputStream(byteIn));
//do something
objectIn.close();
byteIn.close();
}
}