Java File Write via ByteBuffer writeInt(OutputStream os, int val, ByteOrder bo)

Here you can find the source of writeInt(OutputStream os, int val, ByteOrder bo)

Description

Write an int to the OutputStream and advance the stream pointer respectively.

License

Open Source License

Parameter

Parameter Description
os a parameter
val a parameter
bo a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void writeInt(OutputStream os, int val, ByteOrder bo) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w w  w .j  a  v a2  s .c  o  m
   Copyright (c) 2009-2011
  Speech Group at Informatik 5, Univ. Erlangen-Nuremberg, GERMANY
  Korbinian Riedhammer
  Tobias Bocklet
    
   This file is part of the Java Speech Toolkit (JSTK).
    
   The JSTK 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 3 of the License, or
   (at your option) any later version.
    
   The JSTK 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 the JSTK. If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.IOException;

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

public class Main {
    /**
     * Write an int to the OutputStream and advance the stream
     * pointer respectively.
     * @param os
     * @param val
     * @param bo
     * @return
     * @throws IOException
     */
    public static void writeInt(OutputStream os, int val, ByteOrder bo) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE / 8);
        bb.order(bo);
        bb.putInt(val);
        os.write(bb.array());
    }

    /**
     * Write the given int array to the OutputStream using given ByteOrder
     * @param os
     * @param buf
     * @param bo
     * @throws IOException
     */
    public static void writeInt(OutputStream os, int[] buf, ByteOrder bo) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(buf.length * Integer.SIZE / 8);
        bb.order(bo);
        for (int d : buf)
            bb.putInt(d);
        os.write(bb.array());
    }
}

Related

  1. writeFile(String source, File outputFile)
  2. writeFileAsByteArray(File file, byte[] par2Data)
  3. writeFileAsStringArray(File file, String[] par2DataArray)
  4. writeFileNIO(String txt, File fyl)
  5. writeFloat(OutputStream os, float val, ByteOrder bo)
  6. writeInt(OutputStream out, ByteOrder order, int i)
  7. writeInt(OutputStream out, int i)
  8. writeInt(WritableByteChannel channel, int val)
  9. writeInt(WritableByteChannel channel, int value)