Here you can find the source of serializeInt2MinLE(int value)
Parameter | Description |
---|---|
value | a parameter |
public static byte[] serializeInt2MinLE(int value)
//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; } }