X-Git-Url: https://git.hedgecode.org/?p=chesshog.git;a=blobdiff_plain;f=chesshog-qrcode%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fqrcode%2Fzip%2FZipContentUtils.java;fp=chesshog-qrcode%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fqrcode%2Fzip%2FZipContentUtils.java;h=dfd79019181637a92421e605035069d5b3f72e86;hp=0000000000000000000000000000000000000000;hb=6f233e90e111f983e0ef401697859bb677bc0929;hpb=0b24d5b71c19ba38060297084ad39685f76a8539 diff --git a/chesshog-qrcode/src/main/java/org/hedgecode/chess/qrcode/zip/ZipContentUtils.java b/chesshog-qrcode/src/main/java/org/hedgecode/chess/qrcode/zip/ZipContentUtils.java new file mode 100644 index 0000000..dfd7901 --- /dev/null +++ b/chesshog-qrcode/src/main/java/org/hedgecode/chess/qrcode/zip/ZipContentUtils.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2018-2019. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.qrcode.zip; + +import java.util.Arrays; +import java.util.Base64; + +/** + * + * + * @author Dmitry Samoshin aka gotty + */ +public class ZipContentUtils { + + private static final int LENGTH_BYTE_COUNT = 4; + + private static final int MAX_CONTENT_LENGTH = 65536; + + public static int getMaxContentLength() { + return MAX_CONTENT_LENGTH; + } + + public static int getZipLength(byte[] zipBytes) { + return fromByteArray( + Arrays.copyOf(zipBytes, LENGTH_BYTE_COUNT) + ); + } + + public static byte[] getZipBytes(byte[] zipBytes) { + return Arrays.copyOfRange( + zipBytes, LENGTH_BYTE_COUNT, zipBytes.length + ); + } + + public static byte[] concat(int length, byte[] bytes) { + return concat( + toByteArray(length), + bytes + ); + } + + public static String base64Encode(byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + + public static byte[] base64Decode(String value) { + return Base64.getDecoder().decode(value); + } + + private static byte[] toByteArray(int value) { + return new byte[] { + (byte) (value >> 24), + (byte) (value >> 16), + (byte) (value >> 8), + (byte) (value) + }; + } + + private static int fromByteArray(byte[] bytes) { + return bytes[0] << 24 + | (bytes[1] & 0xFF) << 16 + | (bytes[2] & 0xFF) << 8 + | (bytes[3] & 0xFF); + } + + private static byte[] concat(byte[]... byteArrays) { + int length = 0; + for (byte[] bytes : byteArrays) { + length += bytes.length; + } + byte[] concatBytes = new byte[length]; + length = 0; + for (byte[] bytes : byteArrays) { + if (bytes.length == 0) + continue; + System.arraycopy(bytes, 0, concatBytes, length, bytes.length); + length += bytes.length; + } + return concatBytes; + } + + private ZipContentUtils() { + throw new AssertionError( + "No org.hedgecode.chess.qrcode.zip.ZipContentUtils instances!" + ); + } + +}