OSDN Git Service

74bdd8c7fb74452377b83bda126fdcb5b7239f6b
[dictzip-java/dictzip-java.git] / dictzip-lib / src / main / java / org / dict / zip / DictZipFileUtils.java
1 /*
2  * DictZip library.
3  *
4  * Copyright (C) 2016-2022 Hiroshi Miura
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later WITH Classpath-exception-2.0
7  */
8 package org.dict.zip;
9
10 import java.io.EOFException;
11 import java.io.InputStream;
12 import java.io.IOException;
13 import java.io.OutputStream;
14
15
16 /**
17  * Created by Hiroshi Miura on 16/04/09.
18  * @author Hiroshi Miura
19  */
20 public final class DictZipFileUtils {
21
22     static final int CHECK_BUF_LEN = 65536;
23
24     /**
25      * Reads unsigned byte.
26      *
27      * @param in input stream to read.
28      * @return unsigned byte value.
29      * @throws IOException when error in file reading.
30      */
31     static int readUByte(final InputStream in) throws IOException {
32         int b = in.read();
33         if (b == -1) {
34             throw new EOFException();
35         }
36         return b;
37     }
38
39     /**
40      * Reads unsigned integer in Intel byte order.
41      *
42      * @param in input stream to read.
43      * @return unsigned integer value.
44      * @throws IOException when error in file reading.
45      */
46     static long readUInt(final InputStream in) throws IOException {
47         long s = readUShort(in);
48         return ((long) readUShort(in) << 16) | s;
49     }
50
51     /**
52      * Reads unsigned short in Intel byte order.
53      *
54      * @param in input stream to read.
55      * @return unsigned short value.
56      * @throws IOException when error in file reading.
57      */
58     static int readUShort(final InputStream in) throws IOException {
59         int b = readUByte(in);
60         return (readUByte(in) << 8) | b;
61     }
62
63     /**
64      * Writes integer in Intel byte order.
65      *
66      * @param out output stream to write.
67      * @param i   integer to write.
68      * @throws IOException when error in file output.
69      */
70     static void writeInt(final OutputStream out, final int i) throws IOException {
71         writeShort(out, i & 0xffff);
72         writeShort(out, (i >> 16) & 0xffff);
73     }
74
75     /**
76      * Writes short integer in Intel byte order.
77      *
78      * @param out output stream to write.
79      * @param s   short integer to write.
80      * @throws IOException when error in file output.
81      */
82     static void writeShort(final OutputStream out, final int s) throws IOException {
83         out.write(s & 0xff);
84         out.write((s >> 8) & 0xff);
85     }
86
87     /**
88      * Check gzip member stream w/ CRC and length in trailer.
89      * @see DictZipFiles#checkDictZipFile
90      * @param filename to be checked.
91      * @return true if it is a valid dictzip file, otherwise false.
92      * @throws IOException when CRC error or total length error.
93      */
94     @Deprecated
95     public static boolean checkDictZipInputStream(final String filename) throws IOException {
96         return DictZipFiles.checkDictZipFile(filename);
97     }
98
99     /**
100      * Check gzip member stream w/ CRC and length in trailer.
101      * @see DictZipFiles#checkDictZipInputStream
102      * @param in inputstream to be checked.
103      * @return true if inputstream is a valid dictzip, otherwise false.
104      * @throws IOException when CRC error or total length error.
105      */
106     @Deprecated
107     public static boolean checkDictZipInputStream(final DictZipInputStream in) throws IOException {
108         return DictZipFiles.checkDictZipInputStream(in);
109     }
110
111     private DictZipFileUtils() {
112     }
113 }