4 * Copyright (C) 2016-2022 Hiroshi Miura
6 * SPDX-License-Identifier: GPL-2.0-or-later WITH Classpath-exception-2.0
10 import java.io.EOFException;
11 import java.io.InputStream;
12 import java.io.IOException;
13 import java.io.OutputStream;
17 * Created by Hiroshi Miura on 16/04/09.
18 * @author Hiroshi Miura
20 public final class DictZipFileUtils {
22 static final int CHECK_BUF_LEN = 65536;
25 * Reads unsigned byte.
27 * @param in input stream to read.
28 * @return unsigned byte value.
29 * @throws IOException when error in file reading.
31 static int readUByte(final InputStream in) throws IOException {
34 throw new EOFException();
40 * Reads unsigned integer in Intel byte order.
42 * @param in input stream to read.
43 * @return unsigned integer value.
44 * @throws IOException when error in file reading.
46 static long readUInt(final InputStream in) throws IOException {
47 long s = readUShort(in);
48 return ((long) readUShort(in) << 16) | s;
52 * Reads unsigned short in Intel byte order.
54 * @param in input stream to read.
55 * @return unsigned short value.
56 * @throws IOException when error in file reading.
58 static int readUShort(final InputStream in) throws IOException {
59 int b = readUByte(in);
60 return (readUByte(in) << 8) | b;
64 * Writes integer in Intel byte order.
66 * @param out output stream to write.
67 * @param i integer to write.
68 * @throws IOException when error in file output.
70 static void writeInt(final OutputStream out, final int i) throws IOException {
71 writeShort(out, i & 0xffff);
72 writeShort(out, (i >> 16) & 0xffff);
76 * Writes short integer in Intel byte order.
78 * @param out output stream to write.
79 * @param s short integer to write.
80 * @throws IOException when error in file output.
82 static void writeShort(final OutputStream out, final int s) throws IOException {
84 out.write((s >> 8) & 0xff);
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.
95 public static boolean checkDictZipInputStream(final String filename) throws IOException {
96 return DictZipFiles.checkDictZipFile(filename);
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.
107 public static boolean checkDictZipInputStream(final DictZipInputStream in) throws IOException {
108 return DictZipFiles.checkDictZipInputStream(in);
111 private DictZipFileUtils() {