I'm learning about socket programming in Java. I've seen client/server app examples with some using DataOutputStream
, and some using ObjectOutputStream
.
What's the difference between the two?
Is there a performance difference?
You use the DataOutputStream to write the data to e.g. a file, and then use the DataInputStream to read the data again.
A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.
DataOutputStream can only handle basic types. It can only read/write primtive types and Strings. DataInput/OutputStream performs generally better because its much simpler. ObjectInput/OutputStream can read/write any object type was well as primitives.
Java DataOutputStream class allows an application to write primitive Java data types to the output stream in a machine-independent way. Java application generally uses the data output stream to write data that can later be read by a data input stream.
DataInput/OutputStream performs generally better because its much simpler. It can only read/write primtive types and Strings.
ObjectInput/OutputStream can read/write any object type was well as primitives. It is less efficient but much easier to use if you want to send complex data.
I would assume that the Object*Stream is the best choice until you know that its performance is an issue.
This might be useful for people still looking for answers several years later... According to my tests on a recent JVM (1.8_51), the ObjectOutput/InputStream
is surprisingly almost 2x times faster than DataOutput/InputStream
for reading/writing a huge array of double!
Below are the results for writing 10 million items array (for 1 million the results are the essentially the same). I also included the text format (BufferedWriter/Reader) for the sake of completeness:
TestObjectStream written 10000000 items, took: 409ms, or 24449.8778 items/ms, filesize 80390629b TestDataStream written 10000000 items, took: 727ms, or 13755.1582 items/ms, filesize 80000000b TestBufferedWriter written 10000000 items, took: 13700ms, or 729.9270 items/ms, filesize 224486395b
Reading:
TestObjectStream read 10000000 items, took: 250ms, or 40000.0000 items/ms, filesize 80390629b TestDataStream read 10000000 items, took: 424ms, or 23584.9057 items/ms, filesize 80000000b TestBufferedWriter read 10000000 items, took: 6298ms, or 1587.8057 items/ms, filesize 224486395b
I believe Oracle has heavily optimized the JVM for using ObjectStream
s in last Java releases, as this is the most common way of writing/reading data (including serialization), and thus is located on the Java performance critical path.
So looks like today there's no much reason anymore to use DataStream
s. "Don't try to outsmart JVM", just use the most straightforward way, which is ObjectStream
s :)
Here's the code for the test:
class Generator { private int seed = 1235436537; double generate(int i) { seed = (seed + 1235436537) % 936855463; return seed / (i + 1.) / 524323.; } } class Data { public final double[] array; public Data(final double[] array) { this.array = array; } } class TestObjectStream { public void write(File dest, Data data) { try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest)))) { for (int i = 0; i < data.array.length; i++) { out.writeDouble(data.array[i]); } } catch (IOException e) { throw new RuntimeIoException(e); } } public void read(File dest, Data data) { try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dest)))) { for (int i = 0; i < data.array.length; i++) { data.array[i] = in.readDouble(); } } catch (IOException e) { throw new RuntimeIoException(e); } } } class TestDataStream { public void write(File dest, Data data) { try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)))) { for (int i = 0; i < data.array.length; i++) { out.writeDouble(data.array[i]); } } catch (IOException e) { throw new RuntimeIoException(e); } } public void read(File dest, Data data) { try (DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(dest)))) { for (int i = 0; i < data.array.length; i++) { data.array[i] = in.readDouble(); } } catch (IOException e) { throw new RuntimeIoException(e); } } } class TestBufferedWriter { public void write(File dest, Data data) { try (BufferedWriter out = new BufferedWriter(new FileWriter(dest))) { for (int i = 0; i < data.array.length; i++) { out.write(Double.toString(data.array[i])); out.newLine(); } } catch (IOException e) { throw new RuntimeIoException(e); } } public void read(File dest, Data data) { try (BufferedReader in = new BufferedReader(new FileReader(dest))) { String line = in.readLine(); int i = 0; while (line != null) { if(!line.isEmpty()) { data.array[i++] = Double.parseDouble(line); } line = in.readLine(); } } catch (IOException e) { throw new RuntimeIoException(e); } } } @Test public void testWrite() throws Exception { int N = 10000000; double[] array = new double[N]; Generator gen = new Generator(); for (int i = 0; i < array.length; i++) { array[i] = gen.generate(i); } Data data = new Data(array); Map<Class, BiConsumer<File, Data>> subjects = new LinkedHashMap<>(); subjects.put(TestDataStream.class, new TestDataStream()::write); subjects.put(TestObjectStream.class, new TestObjectStream()::write); subjects.put(TestBufferedWriter.class, new TestBufferedWriter()::write); subjects.forEach((aClass, fileDataBiConsumer) -> { File f = new File("test." + aClass.getName()); long start = System.nanoTime(); fileDataBiConsumer.accept(f, data); long took = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); System.out.println(aClass.getSimpleName() + " written " + N + " items, took: " + took + "ms, or " + String.format("%.4f", (N / (double)took)) + " items/ms, filesize " + f.length() + "b"); }); } @Test public void testRead() throws Exception { int N = 10000000; double[] array = new double[N]; Data data = new Data(array); Map<Class, BiConsumer<File, Data>> subjects = new LinkedHashMap<>(); subjects.put(TestDataStream.class, new TestDataStream()::read); subjects.put(TestObjectStream.class, new TestObjectStream()::read); subjects.put(TestBufferedWriter.class, new TestBufferedWriter()::read); subjects.forEach((aClass, fileDataBiConsumer) -> { File f = new File("test." + aClass.getName()); long start = System.nanoTime(); fileDataBiConsumer.accept(f, data); long took = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); System.out.println(aClass.getSimpleName() + " read " + N + " items, took: " + took + "ms, or " + String.format("%.4f", (N / (double)took)) + " items/ms, filesize " + f.length() + "b"); }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With