Changed license to LGPLv3 or later.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / data / EnhancedDataOutputStream.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.data;
11
12 import java.io.DataOutputStream;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.util.List;
16
17 public class EnhancedDataOutputStream extends DataOutputStream {
18
19         public EnhancedDataOutputStream(final OutputStream out) {
20                 super(out);
21         }
22
23         public void writeIntegerList(final List<Integer> list) throws IOException {
24                 writeInt(list.size());
25
26                 for (final Integer integer : list)
27                         writeInt(integer);
28         }
29
30         public void writeString(final String string) throws IOException {
31                 if (string == null) {
32                         writeInt(-1);
33                         return;
34                 }
35
36                 final byte[] bytes = string.getBytes("UTF-8");
37
38                 writeInt(bytes.length);
39                 write(bytes);
40         }
41
42 }