[LIB-9] Add ability to archive qrcode content
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / zip / ZipContentUtils.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.qrcode.zip;
18
19 import java.util.Arrays;
20 import java.util.Base64;
21
22 /**
23  *
24  *
25  * @author Dmitry Samoshin aka gotty
26  */
27 public class ZipContentUtils {
28
29     private static final int LENGTH_BYTE_COUNT = 4;
30
31     private static final int MAX_CONTENT_LENGTH = 65536;
32
33     public static int getMaxContentLength() {
34         return MAX_CONTENT_LENGTH;
35     }
36
37     public static int getZipLength(byte[] zipBytes) {
38         return fromByteArray(
39                 Arrays.copyOf(zipBytes, LENGTH_BYTE_COUNT)
40         );
41     }
42
43     public static byte[] getZipBytes(byte[] zipBytes) {
44         return Arrays.copyOfRange(
45                 zipBytes, LENGTH_BYTE_COUNT, zipBytes.length
46         );
47     }
48
49     public static byte[] concat(int length, byte[] bytes) {
50         return concat(
51                 toByteArray(length),
52                 bytes
53         );
54     }
55
56     public static String base64Encode(byte[] bytes) {
57         return Base64.getEncoder().encodeToString(bytes);
58     }
59
60     public static byte[] base64Decode(String value) {
61         return Base64.getDecoder().decode(value);
62     }
63
64     private static byte[] toByteArray(int value) {
65         return new byte[] {
66                 (byte) (value >> 24),
67                 (byte) (value >> 16),
68                 (byte) (value >> 8),
69                 (byte) (value)
70         };
71     }
72
73     private static int fromByteArray(byte[] bytes) {
74         return bytes[0] << 24
75                 | (bytes[1] & 0xFF) << 16
76                 | (bytes[2] & 0xFF) << 8
77                 | (bytes[3] & 0xFF);
78     }
79
80     private static byte[] concat(byte[]... byteArrays) {
81         int length = 0;
82         for (byte[] bytes : byteArrays) {
83             length += bytes.length;
84         }
85         byte[] concatBytes = new byte[length];
86         length = 0;
87         for (byte[] bytes : byteArrays) {
88             if (bytes.length == 0)
89                 continue;
90             System.arraycopy(bytes, 0, concatBytes, length, bytes.length);
91             length += bytes.length;
92         }
93         return concatBytes;
94     }
95
96     private ZipContentUtils() {
97         throw new AssertionError(
98                 "No org.hedgecode.chess.qrcode.zip.ZipContentUtils instances!"
99         );
100     }
101
102 }