Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//PduUtils is distributed under the terms of the Apache License version 2.0

import java.util.*;

public class Main {
    public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes) {
        return encodedSeptetsToUnencodedSeptets(octetBytes, true);
    }

    public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes, boolean discardLast) {
        byte newBytes[];
        BitSet bitSet;
        int i, j, value1, value2;
        bitSet = new BitSet(octetBytes.length * 8);
        value1 = 0;
        for (i = 0; i < octetBytes.length; i++)
            for (j = 0; j < 8; j++) {
                value1 = (i * 8) + j;
                if ((octetBytes[i] & (1 << j)) != 0)
                    bitSet.set(value1);
            }
        value1++;
        value2 = value1 / 7 + ((value1 % 7 != 0) ? 1 : 0); // big diff here
        if (value2 == 0)
            value2++;
        newBytes = new byte[value2];
        for (i = 0; i < value2; i++) {
            for (j = 0; j < 7; j++) {
                if ((value1 + 1) > (i * 7 + j)) {
                    if (bitSet.get(i * 7 + j)) {
                        newBytes[i] |= (byte) (1 << j);
                    }
                }
            }
        }
        if (discardLast && octetBytes.length * 8 % 7 > 0) {
            // when decoding a 7bit encoded string 
            // the last septet may become 0, this should be discarded
            // since this is an artifact of the encoding not part of the 
            // original string
            // this is only done for decoding 7bit encoded text NOT for
            // reversing octets to septets (e.g. for the encoding the UDH)
            if (newBytes[newBytes.length - 1] == 0) {
                byte[] retVal = new byte[newBytes.length - 1];
                System.arraycopy(newBytes, 0, retVal, 0, retVal.length);
                return retVal;
            }
        }
        return newBytes;
    }
}