net.longfalcon.newsj.util.EncodingUtil.java Source code

Java tutorial

Introduction

Here is the source code for net.longfalcon.newsj.util.EncodingUtil.java

Source

/*
 * Copyright (c) 2016. Sten Martinez
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package net.longfalcon.newsj.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * User: Sten Martinez
 * Date: 10/7/15
 * Time: 10:41 PM
 */
public class EncodingUtil {
    public static final String BLANK_STRING = "";

    private static final String _UTF8 = "UTF-8";

    private static MessageDigest _md5Digest = null;
    private static MessageDigest _sha1Digest = null;
    private static final Log _log = LogFactory.getLog(EncodingUtil.class);

    public static String md5Hash(String input) {
        if (_md5Digest == null) {
            try {
                _md5Digest = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                // this will not fail.
                e.printStackTrace();
                return null;
            }
        }

        byte[] digest = _md5Digest.digest(input.getBytes());

        StringBuilder sb = new StringBuilder();

        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }

        return sb.toString();
    }

    /**
     * TODO: move to better algorithm?
     * @param input
     * @return
     */
    public static String sha1Hash(String input) {
        if (_sha1Digest == null) {
            try {
                _sha1Digest = MessageDigest.getInstance("SHA-1");
            } catch (NoSuchAlgorithmException e) {
                // this will not fail.
                e.printStackTrace();
                return null;
            }
        }

        byte[] digest = _sha1Digest.digest(input.getBytes());

        StringBuilder sb = new StringBuilder();

        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }

        return sb.toString();
    }

    public static String urlEncode(String input) {

        try {
            return URLEncoder.encode(input, _UTF8);
        } catch (UnsupportedEncodingException e) {
            _log.error(e);
        }
        return BLANK_STRING;
    }

    public static String bytesToStringUTFCustom(byte[] bytes) {
        char[] buffer = new char[bytes.length >> 1];
        for (int i = 0; i < buffer.length; i++) {
            int bpos = i << 1;
            char c = (char) (((bytes[bpos] & 0x00FF) << 8) + (bytes[bpos + 1] & 0x00FF));
            buffer[i] = c;
        }
        return new String(buffer);
    }

public static String cp437toUTF(byte[] input) {

   /* // Create the encoder and decoder for cp437
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();

    String s = "";
    try {
        // Convert a string to cp437 bytes in a ByteBuffer
        // The new ByteBuffer is ready to be read.
        ByteBuffer bbuf = ByteBuffer.wrap(input);

        // Convert cp437 bytes in a ByteBuffer to a character ByteBuffer and then to a string.
        // The new ByteBuffer is ready to be read.
        CharBuffer cbuf = decoder.decode(bbuf);
        s = cbuf.toString();
    } catch (CharacterCodingException e) {
        _log.error("Error converting cp437 to UTF", e);
    }*/

    StringBuilder stringBuilder = new StringBuilder();


    for (int i = 0; i < input.length; i++) {
        int b = input[i];
        if (b < 0) {
            b = b + 256;
        }
        char ch = (char) b;
        //System.out.print(ch);
        switch(ch){
            case 128: stringBuilder.append('');break;
            case 129: stringBuilder.append('');break;
            case 130: stringBuilder.append('');break;
            case 131: stringBuilder.append('');break;
            case 132: stringBuilder.append('');break;
            case 133: stringBuilder.append('');break;
            case 134: stringBuilder.append('');break;
            case 135: stringBuilder.append('');break;
            case 136: stringBuilder.append('');break;
            case 137: stringBuilder.append('');break;
            case 138: stringBuilder.append('');break;
            case 139: stringBuilder.append('');break;
            case 140: stringBuilder.append('');break;
            case 141: stringBuilder.append('');break;
            case 142: stringBuilder.append('');break;
            case 143: stringBuilder.append('');break;
            case 144: stringBuilder.append('');break;
            case 145: stringBuilder.append('');break;
            case 146: stringBuilder.append('');break;
            case 147: stringBuilder.append('');break;
            case 148: stringBuilder.append('');break;
            case 149: stringBuilder.append('');break;
            case 150: stringBuilder.append('');break;
            case 151: stringBuilder.append('');break;
            case 152: stringBuilder.append('');break;
            case 153: stringBuilder.append('');break;
            case 154: stringBuilder.append('');break;
            case 155: stringBuilder.append('');break;
            case 156: stringBuilder.append('');break;
            case 157: stringBuilder.append('');break;
            case 158: stringBuilder.append('');break;
            case 159: stringBuilder.append('');break;
            case 160: stringBuilder.append('');break;
            case 161: stringBuilder.append('');break;
            case 162: stringBuilder.append('');break;
            case 163: stringBuilder.append('');break;
            case 164: stringBuilder.append('');break;
            case 165: stringBuilder.append('');break;
            case 166: stringBuilder.append('');break;
            case 167: stringBuilder.append('');break;
            case 168: stringBuilder.append('');break;
            case 169: stringBuilder.append('?');break;
            case 170: stringBuilder.append('');break;
            case 171: stringBuilder.append('');break;
            case 172: stringBuilder.append('');break;
            case 173: stringBuilder.append('');break;
            case 174: stringBuilder.append('');break;
            case 175: stringBuilder.append('');break;
            case 176: stringBuilder.append('');break;
            case 177: stringBuilder.append('');break;
            case 178: stringBuilder.append('');break;
            case 179: stringBuilder.append('');break;
            case 180: stringBuilder.append('');break;
            case 181: stringBuilder.append('');break;
            case 182: stringBuilder.append('');break;
            case 183: stringBuilder.append('');break;
            case 184: stringBuilder.append('');break;
            case 185: stringBuilder.append('');break;
            case 186: stringBuilder.append('');break;
            case 187: stringBuilder.append('');break;
            case 188: stringBuilder.append('?');break;
            case 189: stringBuilder.append('');break;
            case 190: stringBuilder.append('');break;
            case 191: stringBuilder.append('?');break;
            case 192: stringBuilder.append('');break;
            case 193: stringBuilder.append('');break;
            case 194: stringBuilder.append('');break;
            case 195: stringBuilder.append('');break;
            case 196: stringBuilder.append('');break;
            case 197: stringBuilder.append('');break;
            case 198: stringBuilder.append('');break;
            case 199: stringBuilder.append('');break;
            case 200: stringBuilder.append('');break;
            case 201: stringBuilder.append('');break;
            case 202: stringBuilder.append('');break;
            case 203: stringBuilder.append('');break;
            case 204: stringBuilder.append('');break;
            case 205: stringBuilder.append('?');break;
            case 206: stringBuilder.append('');break;
            case 207: stringBuilder.append('');break;
            case 208: stringBuilder.append('');break;
            case 209: stringBuilder.append('');break;
            case 210: stringBuilder.append('');break;
            case 211: stringBuilder.append('');break;
            case 212: stringBuilder.append('');break;
            case 213: stringBuilder.append('');break;
            case 214: stringBuilder.append('');break;
            case 215: stringBuilder.append('');break;
            case 216: stringBuilder.append('');break;
            case 217: stringBuilder.append('');break;
            case 218: stringBuilder.append('');break;
            case 219: stringBuilder.append('');break;
            case 220: stringBuilder.append('');break;
            case 221: stringBuilder.append('');break;
            case 222: stringBuilder.append('?');break;
            case 223: stringBuilder.append('');break;
            case 224: stringBuilder.append('');break;
            case 225: stringBuilder.append('');break;
            case 226: stringBuilder.append('');break;
            case 227: stringBuilder.append('');break;
            case 228: stringBuilder.append('');break;
            case 229: stringBuilder.append('');break;
            case 230: stringBuilder.append('');break;
            case 231: stringBuilder.append('');break;
            case 232: stringBuilder.append('');break;
            case 233: stringBuilder.append('');break;
            case 234: stringBuilder.append('');break;
            case 235: stringBuilder.append('');break;
            case 236: stringBuilder.append('');break;
            case 237: stringBuilder.append('');break;
            case 238: stringBuilder.append('');break;
            case 239: stringBuilder.append('');break;
            case 240: stringBuilder.append('');break;
            case 241: stringBuilder.append('');break;
            case 242: stringBuilder.append('');break;
            case 243: stringBuilder.append('');break;
            case 244: stringBuilder.append('');break;
            case 245: stringBuilder.append('');break;
            case 246: stringBuilder.append('');break;
            case 247: stringBuilder.append('');break;
            case 248: stringBuilder.append('');break;
            case 249: stringBuilder.append('');break;
            case 250: stringBuilder.append('');break;
            case 251: stringBuilder.append('');break;
            case 252: stringBuilder.append('?');break;
            case 253: stringBuilder.append('');break;
            case 254: stringBuilder.append('');break;
            case 255: stringBuilder.append(' ');break;
            default :
                stringBuilder.append(ch);
        }
    }

    return stringBuilder.toString();
}
}