Java examples for java.nio:IntBuffer
Write the given int value into the buffer, starting at the offset value.
/*/*from w w w . java2 s . c o m*/ * Copyright 2012 The Portico Project * * This file is part of portico. * * portico is free software; you can redistribute it and/or modify * it under the terms of the Common Developer and Distribution License (CDDL) * as published by Sun Microsystems. For more information see the LICENSE file. * * Use of this software is strictly AT YOUR OWN RISK!!! * If something bad happens you do not have permission to come crying to me. * (that goes for your lawyer as well) * */ public class Main{ public static void main(String[] argv) throws Exception{ int value = 2; byte[] buffer = new byte[]{34,35,36,37,37,37,67,68,69}; int offset = 2; putIntLE(value,buffer,offset); } /** * Write the given int value into the buffer, starting at the offset value. * <p/> * If there are less than 4 bytes from the offset to the length of the array, a * {@link BufferOverflowException} will be thrown for the buffer overflow. */ public static void putIntLE(int value, byte[] buffer, int offset) { checkOverflow(4, buffer, offset); buffer[offset + 3] = (byte) (value >> 24); buffer[offset + 2] = (byte) (value >> 16); buffer[offset + 1] = (byte) (value >> 8); buffer[offset] = (byte) (value /*>> 0*/); } /** * Check that the required number of bytes are present in the array, starting from * the provided offset. If not, throw a {@link BufferOverflowException}. * * @param required The number of bytes we expect to write * @param buffer The buffer to check * @param offset The offset to start from */ public static void checkOverflow(int required, byte[] buffer, int offset) throws BufferOverflowException { int length = buffer.length - offset; if (length < required) { throw new BufferOverflowException( "Buffer overflow. Tried to write " + required + " bytes to buffer, only " + length + " bytes available"); } } }