parse String By Bytes and encoding - Java java.lang

Java examples for java.lang:byte Array to String

Description

parse String By Bytes and encoding

Demo Code

//package com.java2s;

import java.util.Arrays;

public class Main {
    public static void main(String[] argv) {
        String raw = "java2s.com";
        int len = 4;
        String encoding = "ASCII";
        System.out.println(parseStringByBytes(raw, len, encoding));
    }//www . j av  a2s.  c  o  m

    public static String parseStringByBytes(String raw, int len,
            String encoding) {

        if (raw == null)
            return null;
        String[] ary = null;
        try {
            // raw ??? byte
            byte[] rawBytes = raw.getBytes(encoding);
            int rawLength = rawBytes.length;

            int index = 0;
            int minus_byte_num = 0;
            int offset = 0;

            int hangul_byte_num = encoding.equals("UTF-8") ? 3 : 2;

            if (rawLength > len) {
                int aryLength = (rawLength / len)
                        + (rawLength % len != 0 ? 1 : 0);
                ary = new String[aryLength];

                for (int i = 0; i < aryLength; i++) {
                    minus_byte_num = 0;
                    offset = len;
                    if (index + offset > rawBytes.length) {
                        offset = rawBytes.length - index;
                    }
                    for (int j = 0; j < offset; j++) {
                        if (((int) rawBytes[index + j] & 0x80) != 0) {
                            minus_byte_num++;
                        }
                    }
                    if (minus_byte_num % hangul_byte_num != 0) {
                        offset -= minus_byte_num % hangul_byte_num;
                    }
                    ary[i] = new String(rawBytes, index, offset, encoding);
                    index += offset;

                }
            } else {
                ary = new String[] { raw };
            }
        } catch (Exception e) {

        }
        return Arrays.toString(ary);
    }
}

Related Tutorials