org.apache.niolex.commons.codec.KVBase64Util.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.niolex.commons.codec.KVBase64Util.java

Source

/**
 * KVBase64Util.java
 *
 * Copyright 2012 The original author or authors.
 *
 * 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.apache.niolex.commons.codec;

import org.apache.commons.codec.binary.Base64;
import org.apache.niolex.commons.bean.Pair;

/**
 * KVBase64Util?byte64?
 * KVBase64Util?Base64?apachecommons
 * ?commons-codec-1.3.jar+
 *
 * ???
 * 1. public static String kvToBase64(byte[] key, byte[] value)
 * KVbyte??64
 *
 * 2. public static Pair<byte[], byte[]> base64toKV(String str)
 * 64??KV byte
 *
 * @see org.apache.commons.codec.binary.Base64
 * @author <a href="mailto:xiejiyun@gmail.com">Xie, Jiyun</a>
 * @version 1.0.5
 * @since 2012-12-27
 */
public class KVBase64Util {

    /**
     * KVbyte??64
     * Encode KV Byte Array into Base64 String
     *
     * @param key ?key?63
     * @param value ?value?key+value512
     * @return ??64
     * @throws IllegalStateException ???ASCII?
     */
    public static String kvToBase64(byte[] key, byte[] value) {
        if (key == null || value == null)
            throw new IllegalArgumentException("The parameter should not be null!");
        int size = key.length + value.length;
        if (key.length > 63 || size > 512) {
            throw new IllegalArgumentException("The KV is too large!");
        }
        byte data[] = new byte[(size + 3) / 3 * 3];
        int start = data.length - size;
        int first = (key.length << 2) + start;
        data[0] = (byte) first;
        System.arraycopy(key, 0, data, start, key.length);
        System.arraycopy(value, 0, data, start + key.length, value.length);
        return StringUtil.asciiByteToStr(Base64.encodeBase64URLSafe(data));
    }

    /**
     * 64??KV byte
     * Decode Base64 String into KV Byte Array
     *
     * @param str ?64
     * @return ??KV byte,aKeybValue
     * @throws IllegalStateException ???ASCII?
     */
    public static Pair<byte[], byte[]> base64toKV(String str) {
        if (str == null)
            throw new IllegalArgumentException("The parameter should not be null!");
        byte data[] = Base64.decodeBase64(StringUtil.strToAsciiByte(str));
        int first = data[0] & 0xff;
        int start = first & 0x3;
        first = first >> 2;
        byte key[] = new byte[first];
        first = data.length - first - start;
        if (first < 0) {
            throw new IllegalArgumentException("The parameter is not KV encoded!");
        }
        byte value[] = new byte[first];
        System.arraycopy(data, start, key, 0, key.length);
        System.arraycopy(data, data.length - first, value, 0, value.length);
        return new Pair<byte[], byte[]>(key, value);
    }

}