This function encodes a byte array using base 32 with no indentation of new lines. - Java java.lang

Java examples for java.lang:byte Array Encode Decode

Description

This function encodes a byte array using base 32 with no indentation of new lines.

Demo Code

/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(encode(bytes));
    }/*from   w w  w  .  j  a va2  s  .  c  o m*/

    static private final String lookupTable = "0123456789ABCDFGHJKLMNPQRSTVWXYZ";

    /**
     * This function encodes a byte array using base 32 with no indentation of new lines.
     *
     * @param bytes The byte array to be encoded.
     * @return The base 32 encoded string.
     */
    static public String encode(byte[] bytes) {
        return encode(bytes, null);
    }

    /**
     * This function encodes a byte array using base 32 with a specific indentation of new lines.
     *
     * @param bytes The byte array to be encoded.
     * @param indentation The indentation string to be inserted before each new line.
     * @return The base 32 encoded string.
     */
    static public String encode(byte[] bytes, String indentation) {
        StringBuilder result = new StringBuilder();
        int length = bytes.length;
        if (length == 0)
            return ""; // empty byte array
        if (indentation != null && length > 50) {
            result.append("\n");
            result.append(indentation);
        }
        encodeBytes(bytes[0], bytes[0], 0, result);
        for (int i = 1; i < length; i++) {
            if (indentation != null && i % 50 == 0) {
                // format to indented 80 character blocks
                result.append("\n");
                result.append(indentation);
            }
            encodeBytes(bytes[i - 1], bytes[i], i, result);
        }
        encodeLastByte(bytes[length - 1], length - 1, result);
        return result.toString();
    }

    static private void encodeBytes(byte previous, byte current,
            int byteIndex, StringBuilder output) {
        int chunk;
        int offset = byteIndex % 5;
        switch (offset) {
        case 0:
            chunk = ((current & 0xF8) >>> 3);
            output.append(lookupTable.charAt(chunk));
            break;
        case 1:
            chunk = (((previous & 0x07) << 2) | ((current & 0xC0) >>> 6));
            output.append(lookupTable.charAt(chunk));
            chunk = ((current & 0x3E) >>> 1);
            output.append(lookupTable.charAt(chunk));
            break;
        case 2:
            chunk = (((previous & 0x01) << 4) | ((current & 0xF0) >>> 4));
            output.append(lookupTable.charAt(chunk));
            break;
        case 3:
            chunk = (((previous & 0x0F) << 1) | ((current & 0x80) >>> 7));
            output.append(lookupTable.charAt(chunk));
            chunk = ((current & 0x7C) >>> 2);
            output.append(lookupTable.charAt(chunk));
            break;
        case 4:
            chunk = (((previous & 0x03) << 3) | ((current & 0xE0) >>> 5));
            output.append(lookupTable.charAt(chunk));
            chunk = (current & 0x1F);
            output.append(lookupTable.charAt(chunk));
            break;
        }
    }

    static private void encodeLastByte(byte last, int byteIndex,
            StringBuilder output) {
        int chunk;
        int offset = byteIndex % 5;
        switch (offset) {
        case 0:
            chunk = ((last & 0x07) << 2);
            output.append(lookupTable.charAt(chunk));
            break;
        case 1:
            chunk = ((last & 0x01) << 4);
            output.append(lookupTable.charAt(chunk));
            break;
        case 2:
            chunk = ((last & 0x0F) << 1);
            output.append(lookupTable.charAt(chunk));
            break;
        case 3:
            chunk = ((last & 0x03) << 3);
            output.append(lookupTable.charAt(chunk));
            break;
        case 4:
            break;
        }
    }
}

Related Tutorials