Java Convert via ByteBuffer StringToData(String conv, boolean isRe, int number)

Here you can find the source of StringToData(String conv, boolean isRe, int number)

Description

String To Data

License

Open Source License

Declaration

public static Vector<Object[]> StringToData(String conv, boolean isRe, int number) 

Method Source Code

//package com.java2s;
/**//from   www.  ja v a 2s. c  om
 *   Copyright (c) 2011-12 G Lynch. All Rights Reserved.
 *
 *   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 (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., 675 Mass Ave, Cambridge, MA 02139, USA.
 **/

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.Vector;

public class Main {
    public static Vector<Object[]> StringToData(String conv, boolean isRe, int number) {
        String alpha = "abcdefghikjlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ23456789";
        int counter = 0;
        Vector<Object[]> va = new Vector<Object[]>();

        String[] lines = conv.split("\n");

        Object[] oa;
        String[] sa;
        for (int i = 0; i < lines.length; i++) {
            //System.out.println(lines[i] + "<END>");
            if (lines[i].indexOf("\t") > -1) {
                sa = lines[i].split("\t");

                if (sa.length == 3) {
                    sa[0] = removeLongWhiteSpace(sa[0]);
                    sa[1] = removeLongWhiteSpace(sa[1]);
                    sa[2] = removeLongWhiteSpace(sa[2]);
                    oa = new Object[6];
                    oa[0] = sa[0];
                    oa[1] = sa[1];
                    oa[2] = sa[2];
                    oa[3] = new Boolean(false);
                    oa[4] = Integer.toString(counter);
                    if (isRe) {
                        if (number < alpha.length()) {
                            oa[5] = Integer.toString(counter) + " " + "(" + alpha.charAt(number - 1) + ")";
                        } else {
                            oa[5] = Integer.toString(counter) + " " + "("
                                    + alpha.charAt((int) Math.round(Math.random())) + ")";
                        }
                    } else {
                        oa[5] = Integer.toString(counter);
                    }
                    va.add(oa);
                    counter++;
                } else {
                    for (int j = 0; j < sa.length; j++) {
                        System.out.println(sa[j] + " " + j);
                    }
                }
            }
        }
        return va;
    }

    public static String removeLongWhiteSpace(String s) {
        s = s.trim();
        while (s.contains("  ")) // two white spaces
        {
            s = s.replaceAll("  ", " "); // the first arg should contain 2 spaces, the second only 1
        }
        //System.out.println("Length before UTF8 cleaning " + s.length());
        CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
        utf8Decoder.onMalformedInput(CodingErrorAction.IGNORE);
        utf8Decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
        try {
            ByteBuffer bytes;
            bytes = ByteBuffer.wrap(s.getBytes());
            CharBuffer parsed = utf8Decoder.decode(bytes);
            s = parsed.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //This code removes all of the bytes with value of zero
        // Gerard 21st March 2012: Solved, bug with random spaces in segments
        byte[] b = s.getBytes();
        Vector<Byte> vb = new Vector();

        for (int i = 0; i < b.length; i++) {
            if (b[i] != 0) {
                vb.add(b[i]);
            }
        }
        Object[] ba = vb.toArray();
        Byte[] bya = new Byte[ba.length];
        byte[] byta = new byte[ba.length];
        for (int i = 0; i < ba.length; i++) {
            bya[i] = (Byte) ba[i];
            byta[i] = bya[i].byteValue();
        }
        s = new String(byta);
        return s;
    }
}

Related

  1. shortToBytes(short s)
  2. stringsFromBinary(final byte[] binary)
  3. stringsToBinary(final String strings[])
  4. stringToBytes(final String inString)
  5. stringToBytes(String str)
  6. stringToIpAddress(String s)
  7. stringToLong(String offsetString)
  8. stringToLongUnknownLength(String str, int startIndex)
  9. to32BitsLongArray(byte[] data, boolean bigEndian)