Java XML Data Type Converter getTextAsJavaByteArrayInitializer( String text, String charsetName, int maxArraySize)

Here you can find the source of getTextAsJavaByteArrayInitializer( String text, String charsetName, int maxArraySize)

Description

get Text As Java Byte Array Initializer

License

Apache License

Declaration

static public List<String> getTextAsJavaByteArrayInitializer(
            String text, String charsetName, int maxArraySize)
            throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/*//  w  ww.  j  a  v  a  2  s.c  om
 * Copyright 2015 Fizzed Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    static public List<String> getTextAsJavaByteArrayInitializer(
            String text, String charsetName, int maxArraySize)
            throws UnsupportedEncodingException {
        byte[] bytes = text.getBytes(charsetName);

        List<String> arrays = new ArrayList<>();

        for (int length = 0; length < bytes.length;) {

            StringBuilder sb = new StringBuilder();

            sb.append("new byte[] { ");

            for (int chunk = 0; length < bytes.length
                    && chunk < maxArraySize; chunk++) {

                byte b = bytes[length];

                if (chunk != 0) {
                    sb.append(", ");
                }

                appendByteAsJavaByteInitializer(sb, b);

                chunk++;
                length++;
            }

            sb.append(" };");

            arrays.add(sb.toString());
        }

        return arrays;
    }

    static public void appendByteAsJavaByteInitializer(StringBuilder sb,
            byte b) {
        if (b >= 0 && b < 128) {
            if (Character.isAlphabetic(b)) {
                sb.append("'").append((char) b).append("'");
            } else {
                // no cast needed
                sb.append("0x").append(
                        javax.xml.bind.DatatypeConverter
                                .printHexBinary(new byte[] { b }));
            }
        } else {
            // cast needed
            sb.append("(byte)0x").append(
                    javax.xml.bind.DatatypeConverter
                            .printHexBinary(new byte[] { b }));
        }
    }
}

Related

  1. getByteListStr(List byteList, boolean isEncode)
  2. getDataUrlForBytes(final byte[] bytes)
  3. getImgurContent(String clientID, byte[] image)
  4. getLong(byte[] data)
  5. getText(URL url, String user, String password)
  6. getXMLLong(final long value)
  7. isValidValue(String value, Class type)
  8. millis2String(long millis)
  9. mixColumns(byte[] input, boolean inv)