summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/psmb4j/util/InputStreamUtils.java
blob: 7b3fd9cf4ad2929d7d5a6519db00a45e1fe2c71c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.keuin.psmb4j.util;

import com.keuin.psmb4j.util.error.SocketClosedException;
import com.keuin.psmb4j.util.error.StringLengthExceededException;

import java.io.IOException;
import java.io.InputStream;

public class InputStreamUtils {
    /**
     * Read until '\0', the trailing '\0' is dropped.
     */
    public static String readCString(InputStream stream) throws IOException {
        var sb = new StringBuilder();
        int c;
        while ((c = stream.read()) > 0) {
            sb.append((char) c);
        }
        return sb.toString();
    }

    /**
     * Read a C style string, with a length limit.
     * If the string is longer than given limit,
     * a {@link StringLengthExceededException} will be thrown.
     */
    public static String readCString(InputStream stream, long maxLength) throws IOException {
        var sb = new StringBuilder();
        int c;
        long length = 0;
        while ((c = stream.read()) > 0) {
            sb.append((char) c);
            if (++length > maxLength) {
                throw new StringLengthExceededException(maxLength);
            }
        }
        return sb.toString();
    }

    /**
     * Read fixed length bytes from stream.
     * If not enough, a {@link SocketClosedException} will be thrown.
     */
    public static byte[] readBytes(InputStream stream, int length) throws IOException {
        var buffer = new byte[length];
        int c;
        for (int i = 0; i < length; i++) {
            if ((c = stream.read()) >= 0) {
                buffer[i] = (byte) c;
            } else {
                throw new SocketClosedException(length, i);
            }
        }
        return buffer;
    }
}