Java Serialize serializeInt2MinLE(int value)

Here you can find the source of serializeInt2MinLE(int value)

Description

Returns the minimum bytes needed for the given value to encoded it in little-endian format.

License

Open Source License

Parameter

Parameter Description
value a parameter

Declaration

public static byte[] serializeInt2MinLE(int value) 

Method Source Code

//package com.java2s;
/*//from ww  w .j a  va  2 s.co  m
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2008 Phex Development Group
 *
 *  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
 * 
 *  --- SVN Information ---
 *  $Id: IOUtil.java 4416 2009-03-22 17:12:33Z ArneBab $
 */

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Returns the minimum bytes needed for the given value to encoded it in
     * little-endian format. Value must be positive.
     * @param value
     * @return
     */
    public static byte[] serializeInt2MinLE(int value) {
        if (value < 0) {
            throw new IllegalArgumentException("Negative input value");
        }
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(4);
        do {
            byteStream.write(value & 0xFF);
            value >>= 8;
        } while (value != 0);
        return byteStream.toByteArray();
    }

    /**
     * Returns the minimum bytes needed for the given value to encoded it in
     * little-endian format. Value must be positive.
     * @param value
     * @return
     */
    public static byte[] serializeInt2MinLE(long value) {
        if (value <= 0xFFFF) {
            if (value <= 0xFF) {
                if (value < 0) {
                    throw new IllegalArgumentException(
                            "Negative input value");
                }
                return new byte[] { (byte) value };
            }
            return new byte[] { (byte) value, (byte) (value >> 8) };
        }
        if (value <= 0xFFFFFF) {
            return new byte[] { (byte) value, (byte) (value >> 8),
                    (byte) (value >> 16) };
        }
        return new byte[] { (byte) value, (byte) (value >> 8),
                (byte) (value >> 16), (byte) (value >> 24) };
    }

    /**
     * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
     * <p>
     * This method buffers the input internally, so there is no need to use a
     * <code>BufferedInputStream</code>.
     * 
     * @param input  the <code>InputStream</code> to read from
     * @return the requested byte array
     * @throws NullPointerException if the input is null
     * @throws IOException if an I/O error occurs
     */
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copy(input, output);
        return output.toByteArray();
    }

    /**
     * Copy bytes from an <code>InputStream</code> to an
     * <code>OutputStream</code>.
     * <p>
     * This method buffers the input internally, so there is no need to use a
     * <code>BufferedInputStream</code>.
     * 
     * @param input  the <code>InputStream</code> to read from
     * @param output  the <code>OutputStream</code> to write to
     * @return the number of bytes copied
     * @throws NullPointerException if the input or output is null
     * @throws IOException if an I/O error occurs
     * @since 1.1
     */
    public static int copy(InputStream input, OutputStream output)
            throws IOException {
        byte[] buffer = new byte[4 * 1024];
        int count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
}

Related

  1. serializeDistributionType(String distributionType, String HadoopDistribution)
  2. serializeExecutor(Object executor)
  3. serializeFromString(final String pString)
  4. serializeGZip(T obj)
  5. serializeInt(int i)
  6. serializeInt2MinLE(int value)
  7. serializeIntLE(int value, byte[] outbuf, int offset)
  8. serializeJdk(Object obj)
  9. serializeMock(Object mock)