Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.util.Base64;

public class Main {
    public static String _getReadableByteArr(byte[] input) {
        String out = new String(input);
        if (!_isItReadable(out))
            out = _byteArrayToB64(input);
        else
            out = _byteArrayToReadableStr(input);
        return out;
    }

    public static Boolean _isItReadable(String input) {
        int readableChar = 0;
        for (int i = 0; i < input.length(); i++) {
            int c = input.charAt(i);
            if (c >= 32 && c < 127) {
                readableChar++;
            }
        }

        // can be considered readable if X% characters are ascii
        // (0 is considered a character here so that UTF16 
        // can be considered readable too)
        return (readableChar > (input.length() * 0.75) ? true : false);
    }

    public static String _byteArrayToB64(byte[] a) {
        return Base64.encodeToString(a, Base64.NO_WRAP);
    }

    public static String _byteArrayToReadableStr(byte[] a) {
        StringBuilder sb = new StringBuilder();
        for (byte b : a) {
            if (b >= 32 && b < 127)
                sb.append(String.format("%c", b));
            else
                sb.append('.');
        }
        //sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }
}