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;
/*//  w w  w  . j a va  2 s .  c om
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2005 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
 * 
 *  --- CVS Information ---
 *  $Id: IOUtil.java,v 1.36 2005/10/29 18:05:40 gregork Exp $
 */

import java.io.*;

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();
    }

    /**
     * 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. serializeExecutor(Object executor)
  2. serializeFromString(final String pString)
  3. serializeGZip(T obj)
  4. serializeInt(int i)
  5. serializeInt2MinLE(int value)
  6. serializeIntLE(int value, byte[] outbuf, int offset)
  7. serializeJdk(Object obj)
  8. serializeMock(Object mock)
  9. serializeObject(B b)