Java Integer Array Convert To intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length)

Here you can find the source of intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length)

Description

Convert an array of ints into an array of bytes.

License

Open Source License

Parameter

Parameter Description
dst the array to write
dst_offset the start offset in <code>dst</code>
src the array to read
src_offset the start offset in <code>src</code>, times 4. This measures the offset as if <code>src</code> were an array of <code>byte</code>s (rather than <code>int</code>s).
length the number of <code>byte</code>s to copy.

Declaration

public static final void intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length) 

Method Source Code

//package com.java2s;
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
 *
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 *
 * For more information, contact://from ww w .  j a  v  a  2 s.co m
 *
 *  Generalitat Valenciana
 *   Conselleria d'Infraestructures i Transport
 *   Av. Blasco Ib??ez, 50
 *   46010 VALENCIA
 *   SPAIN
 *
 *      +34 963862235
 *   gvsig@gva.es
 *      www.gvsig.gva.es
 *
 *    or
 *
 *   IVER T.I. S.A
 *   Salamanca 50
 *   46005 Valencia
 *   Spain
 *
 *   +34 963163400
 *   dac@iver.es
 */

public class Main {
    public static final int SIZE_INT = 4;

    /**
     * Convert an array of <code>int</code>s into an array of
     * <code>bytes</code>.
     *
     * @param dst the array to write
     * @param dst_offset the start offset in <code>dst</code>
     * @param src the array to read
     * @param src_offset the start offset in <code>src</code>, times 4. This
     *         measures the offset as if <code>src</code> were an array of
     *         <code>byte</code>s (rather than <code>int</code>s).
     * @param length the number of <code>byte</code>s to copy.
     */
    public static final void intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length) {
        if ((src == null) || (dst == null) || ((dst_offset + length) > dst.length)
                || ((src_offset + length) > (src.length * 4)) || ((src_offset % 4) != 0) || ((length % 4) != 0)) {
            croak("intsToBytes parameters are invalid:" + " src=" + src + " dst=" + dst + " (dst_offset="
                    + dst_offset + " + length=" + length + ")=" + (dst_offset + length) + " > dst.length="
                    + ((dst == null) ? 0 : dst.length) + " (src_offset=" + src_offset + " + length=" + length + ")="
                    + (src_offset + length) + " > (src.length=" + ((src == null) ? 0 : src.length) + "*4)="
                    + ((src == null) ? 0 : (src.length * 4)) + " (src_offset=" + src_offset + " % 4)="
                    + (src_offset % 4) + " != 0" + " (length=" + length + " % 4)=" + (length % 4) + " != 0");
        }

        // Convert parameters to normal format
        int[] offset = new int[1];
        offset[0] = dst_offset;

        int int_src_offset = src_offset / 4;

        for (int i = 0; i < (length / 4); ++i) {
            intToBytes(src[int_src_offset++], dst, offset);
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param msg DOCUMENT ME!
     */
    private static void croak(String msg) {
        // throw new java.AssertionViolatedException(msg);
    }

    /**
     * Write the bytes representing <code>i</code> into the byte array
     * <code>data</code>, starting at index <code>offset [0]</code>, and
     * increment <code>offset [0]</code> by the number of bytes written; if
     * <code>data == null</code>, increment <code>offset [0]</code> by the
     * number of bytes that would have been written otherwise.
     *
     * @param i the <code>int</code> to encode
     * @param data The byte array to store into, or <code>null</code>.
     * @param offset A single element array whose first element is the index in
     *         data to begin writing at on function entry, and which on
     *         function exit has been incremented by the number of bytes
     *         written.
     */
    public static final void intToBytes(int i, byte[] data, int[] offset) {
        /**
         * TODO: We use network-order within OceanStore, but temporarily
         * supporting intel-order to work with some JNI code until JNI code is
         * set to interoperate with network-order.
         */
        if (data != null) {
            for (int j = (offset[0] + SIZE_INT) - 1; j >= offset[0]; --j) {
                data[j] = (byte) i;
                i >>= 8;
            }
        }

        offset[0] += SIZE_INT;
    }
}

Related

  1. intArrayToBits(int[] ina, int min, int max, int numBits)
  2. intArrayToCharArray(int[][] intArray)
  3. intArrayToFloatArray(int[] intArray)
  4. intArrayToIp(final int[] array, final int offset)
  5. intsToByteHighAndLow(int highValue, int lowValue)
  6. intsToBytes(int[] a, int aoffset, int len, byte[] b, int boffset)
  7. intsToBytes(int[] data)
  8. intsToBytes(int[] intArray)
  9. intsToBytes(int[][] ints)