Code cleanup and formatting. Migrated to java 1.8.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / data / EnhancedDataInputStream.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.DataInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 public class EnhancedDataInputStream extends DataInputStream {
19
20     public EnhancedDataInputStream(final InputStream in) {
21         super(in);
22     }
23
24     public List<Integer> readIntegerList() throws IOException {
25         final int length = readInt();
26
27         final List<Integer> result = new ArrayList<>();
28
29         for (int i = 0; i < length; i++)
30             result.add(readInt());
31
32         return result;
33     }
34
35     public String readString() throws IOException {
36
37         final int length = readInt();
38         if (length == -1)
39             return null;
40
41         final byte[] bytes = new byte[length];
42         readFully(bytes);
43
44         return new String(bytes, "UTF-8");
45     }
46
47 }