Java File Write via ByteBuffer writeDataLengthsToHeader(FileOutputStream fpo)

Here you can find the source of writeDataLengthsToHeader(FileOutputStream fpo)

Description

write Data Lengths To Header

License

Open Source License

Declaration

public static boolean writeDataLengthsToHeader(FileOutputStream fpo) 

Method Source Code

//package com.java2s;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.nio.channels.FileChannel;

public class Main {
    public static boolean writeDataLengthsToHeader(FileOutputStream fpo) {
        long len;
        ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
        FileChannel fc = fpo.getChannel();
        try {//from  w  w w .j a v a  2 s .  c om
            len = fc.size();

            fc.position(4);
            bb.putInt((int) (len - 8));
            bb.flip();
            fc.write(bb);

            fc.position(40);
            bb.clear();
            bb.putInt((int) (len - 44));
            bb.flip();
            fc.write(bb);
        } catch (IOException e) {
            System.err.println("error writing data lengths.");
            e.printStackTrace(System.err);
            return false;
        }
        return true;
    }
}

Related

  1. write(SeekableByteChannel channel, long start, byte[] bytes)
  2. write24BitInteger(int integer, ByteOrder order)
  3. writeByte(WritableByteChannel channel, byte value)
  4. writeBytes(Path file, byte[] bytes)
  5. writeComment(File zipFile, String comment)
  6. writeDelimitedToOutputStream(byte[] bytes, OutputStream outputStream)
  7. writeDouble(BufferedWriter bw, double[] buf)
  8. writeEmpty()
  9. writeFC(String fname, float[] res)