Java ByteBuffer Write writeInt(ByteBuffer buf, int pos, int v)

Here you can find the source of writeInt(ByteBuffer buf, int pos, int v)

Description

Writes a specific integer value (4 bytes) to the output byte buffer at the given offset.

License

Apache License

Parameter

Parameter Description
buf output byte buffer
pos offset into the byte buffer to write
v int value to write

Declaration

public static void writeInt(ByteBuffer buf, int pos, int v) 

Method Source Code

//package com.java2s;
/*//from   www  .j a  v  a  2 s . com
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the ?License??). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Writes a specific integer value (4 bytes) to the output byte array at the given offset.
     *
     * @param buf output byte array
     * @param pos offset into the byte buffer to write
     * @param v int value to write
     */
    public static void writeInt(byte[] buf, int pos, int v) {
        checkBoundary(buf, pos, 4);
        buf[pos++] = (byte) (0xff & (v >> 24));
        buf[pos++] = (byte) (0xff & (v >> 16));
        buf[pos++] = (byte) (0xff & (v >> 8));
        buf[pos] = (byte) (0xff & v);
    }

    /**
     * Writes a specific integer value (4 bytes) to the output byte buffer at the given offset.
     *
     * @param buf output byte buffer
     * @param pos offset into the byte buffer to write
     * @param v int value to write
     */
    public static void writeInt(ByteBuffer buf, int pos, int v) {
        buf.put(pos++, (byte) (0xff & (v >> 24)));
        buf.put(pos++, (byte) (0xff & (v >> 16)));
        buf.put(pos++, (byte) (0xff & (v >> 8)));
        buf.put(pos, (byte) (0xff & v));
    }

    /**
     * Writes a specific integer value (4 bytes) to the output stream.
     *
     * @param out output stream
     * @param v integer value to write
     * @throws IOException if an I/O error occurs; in particular, an {@code IOException} may be
     *         thrown if the output stream has been closed.
     */
    public static void writeInt(OutputStream out, int v) throws IOException {
        out.write((byte) (0xff & (v >> 24)));
        out.write((byte) (0xff & (v >> 16)));
        out.write((byte) (0xff & (v >> 8)));
        out.write((byte) (0xff & v));
    }

    /**
     * Ensures that the given buffer contains at least the given number of bytes after the given
     * offset.
     *
     * @param buf input byte array
     * @param pos position in the byte array to start writing
     * @param len length of data to write from the given position
     */
    private static void checkBoundary(byte[] buf, int pos, int len) {
        if (pos + len > buf.length) {
            throw new ArrayIndexOutOfBoundsException();
        }
    }
}

Related

  1. writeHeader(int type, ByteBuffer lengthBuffer, ByteBuffer buffer)
  2. writeHeaderAndLsd(ByteBuffer out, int width, int height, boolean hasGct, int gctSize)
  3. writeHexString(ByteBuffer buffer, String hex)
  4. writeHexString(ByteBuffer buffer, String hexStr)
  5. writeImageDescriptor(ByteBuffer out, int imageLeft, int imageTop, int imageWidth, int imageHeight, boolean hasLct, int numColors)
  6. writeInt(ByteBuffer logBuf, int i)
  7. writeInt24(int v, ByteBuffer buffer)
  8. writeInt4(final ByteBuffer bb, int value, int highValue, final boolean flush)
  9. writeInt8(final ByteBuffer bb, int value)