/* * 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!" ); } }