Java Convert via ByteBuffer toBytes(char[] chars)

Here you can find the source of toBytes(char[] chars)

Description

to Bytes

License

Apache License

Declaration

public static byte[] toBytes(char[] chars) 

Method Source Code

//package com.java2s;
/** I re-ported the (slightly modified) HBase's Bytes code here
 * to avoid dependencies on HBase in the common package of DML.
 * The copyright is included.//from  w w  w  .j  ava 2 s .  c  o m
 *
 *
 * Copyright 2010 The Apache Software Foundation
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class Main {
    /***************************************************************************
     * Primitive arrays conversion utilities.
     **************************************************************************/

    /* assuming the characters are encoded in UTF-8 */
    private static final Charset CHARSET = Charset.forName("UTF-8");
    /** I re-ported the (slightly modified) HBase's Bytes code here
     * to avoid dependencies on HBase in the common package of DML.
     * The copyright is included.
     *
     *
     * Copyright 2010 The Apache Software Foundation
     *
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you 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.
     */

    public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE;
    public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE;
    public static final int SIZEOF_SHORT = Short.SIZE / Byte.SIZE;

    public static byte[] toBytes(char[] chars) {
        CharBuffer cf = CharBuffer.wrap(chars);
        ByteBuffer bf = CHARSET.encode(cf);
        return bf.array();
    }

    public static byte[] toBytes(String[] strings) {
        int n = strings.length;

        // avoid creating intermediate objects by incremental concantenation
        byte[][] tmp = new byte[n][];
        int size = SIZEOF_INT * (n + 1);
        for (int i = 0; i < n; i++) {
            tmp[i] = strings[i].getBytes(CHARSET);
            size += tmp[i].length;
        }
        byte[] bytes = new byte[size];
        addToByteArray(n, bytes, 0);
        int offset = SIZEOF_INT;
        for (int i = 0; i < n; i++) {
            addToByteArray(strings[i].length(), bytes, offset);
            offset += SIZEOF_INT;
            System.arraycopy(tmp[i], 0, bytes, offset, tmp[i].length);
            offset += tmp[i].length;
        }
        return bytes;
    }

    public static byte[] toBytes(long[] longs) {
        int size = SIZEOF_INT + longs.length * SIZEOF_LONG;
        byte[] bytes = new byte[size];
        addToByteArray(longs.length, bytes, 0);
        int offset = SIZEOF_INT;
        for (int i = 0; i < longs.length; i++) {
            addToByteArray(longs[i], bytes, offset);
            offset += SIZEOF_LONG;
        }
        return bytes;
    }

    public static byte[] toBytes(int[] ints) {
        int size = SIZEOF_INT + ints.length * SIZEOF_INT;
        byte[] bytes = new byte[size];
        addToByteArray(size, bytes, 0);
        int offset = SIZEOF_INT;
        for (int i = 0; i < ints.length; i++) {
            addToByteArray(ints[i], bytes, offset);
            offset += SIZEOF_INT;
        }
        return bytes;
    }

    public static byte[] toBytes(short[] shorts) {
        int size = shorts.length * SIZEOF_SHORT + SIZEOF_INT;
        byte[] bytes = new byte[size];
        addToByteArray(size, bytes, 0);
        int offset = SIZEOF_INT;
        for (int i = 0; i < shorts.length; i++) {
            addToByteArray(shorts[i], bytes, offset);
            offset += SIZEOF_SHORT;
        }
        return bytes;
    }

    public static byte[] toBytes(boolean[] bools) {
        int size = SIZEOF_INT + bools.length;
        byte[] bytes = new byte[size];
        addToByteArray(size, bytes, 0);
        for (int i = 0; i < bools.length; i++) {
            bytes[i + SIZEOF_INT] = bools[i] ? (byte) 1 : (byte) 0;
        }
        return bytes;
    }

    public static byte[] toBytes(float[] floats) {
        int size = SIZEOF_INT * (floats.length + 1);
        byte[] bytes = new byte[size];
        addToByteArray(size, bytes, 0);
        int offset = SIZEOF_INT;
        for (int i = 0; i < floats.length; i++) {
            addToByteArray(floats[i], bytes, offset);
            offset += SIZEOF_INT;
        }
        return bytes;
    }

    public static byte[] toBytes(double[] doubles) {
        int size = SIZEOF_INT + doubles.length * SIZEOF_LONG;
        byte[] bytes = new byte[size];
        addToByteArray(size, bytes, 0);
        int offset = SIZEOF_INT;
        for (int i = 0; i < doubles.length; i++) {
            addToByteArray(doubles[i], bytes, offset);
            offset += SIZEOF_LONG;
        }
        return bytes;
    }

    /** Tuan - I constructed the whole byte array to avoid unnecessary object creation */
    private static void addToByteArray(long val, byte[] bytes, int offset) {
        int sentinel = SIZEOF_LONG + offset - 1;
        for (int i = sentinel; i > offset; i--) {
            bytes[i] = (byte) val;
            val >>>= 8;
        }
        bytes[offset] = (byte) val;
    }

    private static void addToByteArray(int val, byte[] bytes, int offset) {
        int sentinel = SIZEOF_INT + offset - 1;
        for (int i = sentinel; i > offset; i--) {
            bytes[i] = (byte) val;
            val >>>= 8;
        }
        bytes[offset] = (byte) val;
    }

    private static void addToByteArray(short val, byte[] bytes, int offset) {
        bytes[offset + 1] = (byte) val;
        val >>= 8;
        bytes[offset] = (byte) val;
    }

    private static void addToByteArray(float val, byte[] bytes, int offset) {
        int intBits = Float.floatToRawIntBits(val);
        addToByteArray(intBits, bytes, offset);
    }

    private static void addToByteArray(double val, byte[] bytes, int offset) {
        long longBits = Double.doubleToRawLongBits(val);
        addToByteArray(longBits, bytes, offset);
    }
}

Related

  1. toByteArray3(String filename)
  2. toByteArrayFromInt(int intValue, boolean shortSize)
  3. toByteArrayFromLong(long longValue)
  4. toBytes(BigDecimal number, int byteLength)
  5. toBytes(char[] ch)
  6. toBytes(char[] string)
  7. toBytes(final float val)
  8. toBytes(final UUID uuid)
  9. toBytes(InputStream input)