Java File Write via ByteBuffer writeInt(WritableByteChannel channel, int value)

Here you can find the source of writeInt(WritableByteChannel channel, int value)

Description

Wrtie an integer value to a channel.

License

Apache License

Parameter

Parameter Description
channel the channel to write to.
value the value to write.

Exception

Parameter Description
IOException if an error occurs while writing the data.

Declaration

public static void writeInt(WritableByteChannel channel, int value)
        throws IOException 

Method Source Code

//package com.java2s;
/*/* w  w w .j a v a 2  s . c o m*/
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;

public class Main {
    /**
     * Serialize an int value into an array of bytes.
     * @param value the int value to serialize.
     * @return an array of bytes filled with the value's representation.
     */
    public static byte[] writeInt(int value) {
        return writeInt(value, new byte[4], 0);
    }

    /**
     * Serialize an int value into an array of bytes.
     * @param value the int value to serialize.
     * @param data the array of bytes into which to serialize the value.
     * @param offset the position in the array of byte at which the serializatrion should start.
     * @return an array of bytes filled with the value's representation, starting at the specified offset.
     */
    public static byte[] writeInt(int value, byte[] data, int offset) {
        int pos = offset;
        data[pos++] = (byte) ((value >>> 24) & 0xFF);
        data[pos++] = (byte) ((value >>> 16) & 0xFF);
        data[pos++] = (byte) ((value >>> 8) & 0xFF);
        data[pos++] = (byte) ((value >>> 0) & 0xFF);
        return data;
    }

    /**
     * Serialize an int value to a stream.
     * @param value the int value to serialize.
     * @param os the stream to write to.
     * @throws IOException if an error occurs while writing the data.
     */
    public static void writeInt(int value, OutputStream os)
            throws IOException {
        os.write((byte) ((value >>> 24) & 0xFF));
        os.write((byte) ((value >>> 16) & 0xFF));
        os.write((byte) ((value >>> 8) & 0xFF));
        os.write((byte) ((value >>> 0) & 0xFF));
    }

    /**
     * Wrtie an integer value to a channel.
     * @param channel the channel to write to.
     * @param value the value to write.
     * @throws IOException if an error occurs while writing the data.
     */
    public static void writeInt(WritableByteChannel channel, int value)
            throws IOException {
        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.putInt(value);
        buf.flip();
        int count = 0;
        while (count < 4) {
            int n = 0;
            while (n == 0)
                n = channel.write(buf);
            if (n < 0)
                throw new ClosedChannelException();
            count += n;
            /*
            count += channel.write(buf);
            if (count < 0) throw new ClosedChannelException();
             */
        }
    }
}

Related

  1. writeFloat(OutputStream os, float val, ByteOrder bo)
  2. writeInt(OutputStream os, int val, ByteOrder bo)
  3. writeInt(OutputStream out, ByteOrder order, int i)
  4. writeInt(OutputStream out, int i)
  5. writeInt(WritableByteChannel channel, int val)
  6. writeInt(WritableByteChannel channel, int value)
  7. writeLong(OutputStream in, long i)
  8. writeLong(OutputStream os, Long d)
  9. writeLong(RandomAccessFile raFile, long num)