Java ByteBuffer Write writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log)

Here you can find the source of writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log)

Description

Write received block to physical file on disk.

License

Open Source License

Parameter

Parameter Description
fileName Full path of existing file on disk.
startOffset 0-based start position in file, inclusive.
buffer Buffer holding the block itself, from position(), meaning that its remaining() is the length of data to write.
log a parameter

Return

False if file does not exist or failed to write all bytes from any other reason.

Declaration

public static boolean writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log) 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

import java.nio.channels.WritableByteChannel;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
    /**//from w  w w .  ja  v a 2s  . c  om
     * Write received block to physical file on disk.
     * 
     * @param fileName
     *            Full path of existing file on disk.
     * @param startOffset
     *            0-based start position in file, inclusive.
     * @param buffer
     *            Buffer holding the block itself, from position(), meaning that
     *            its remaining() is the length of data to write.
     * @param log
     * @return False if file does not exist or failed to write all bytes from
     *         any other reason.
     */
    public static boolean writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log) {
        RandomAccessFile randomAccessFile = null;
        File file = new File(fileName);

        //
        // File is supposed to be on disk already, since the file was used for
        // the first time
        //
        if (!file.exists()) {
            if (log != null)
                log.log(Level.WARNING, "Cannot write file \"" + fileName + "\" that does not exist");
            return false;
        }

        //
        // Make sure that position and length match the file size
        //
        if (startOffset + buffer.remaining() > file.length()) {
            if (log != null)
                log.log(Level.SEVERE,
                        "Wrong position and/or length when trying to write file \"" + fileName + "\", file length "
                                + file.length() + ", offset " + startOffset + ", data length "
                                + buffer.remaining());
            return false;
        }

        //
        // Create the file descriptor
        //
        try {
            // It can fail if file does not exist or there is a security problem
            randomAccessFile = new RandomAccessFile(file, "rw");
        } catch (Exception e) {
            if (log != null)
                log.log(Level.WARNING, "Failed to open file \"" + fileName + "\" for write:\n" + e);
            return false;
        }

        //
        // Move writing head to the right position
        //
        try {
            randomAccessFile.seek(startOffset);
        } catch (Exception e) {
            if (log != null)
                log.log(Level.SEVERE, "Failed to set position when trying to write file \"" + fileName
                        + "\", file length " + file.length() + ", offset " + startOffset + ":\n{3}" + e);
            try {
                randomAccessFile.close();
            } catch (Exception e2) {
            }
            return false;
        }

        WritableByteChannel channel = randomAccessFile.getChannel();

        //
        // Write the data
        //
        int dataLength = buffer.remaining();
        int writtenBytes = 0;
        try {
            writtenBytes = channel.write(buffer);
        } catch (IOException e) {
            if (log != null)
                log.log(Level.SEVERE, "Failed to write data to file \"" + fileName + "\", file length "
                        + file.length() + ", offset " + startOffset + ", data length " + dataLength + ":\n" + e);
            try {
                randomAccessFile.close();
            } catch (Exception e2) {
            }
            return false;
        }

        //
        // Close the file
        //
        try {
            randomAccessFile.close();
        } catch (Exception e2) {
        }

        //
        // Check if all bytes were written
        //
        if (writtenBytes != dataLength) {
            if (log != null)
                log.log(Level.SEVERE, "Failed to write data to file \"" + fileName + "\", wrote only "
                        + writtenBytes + " instead of " + dataLength);
            return false;
        }

        return true;
    }
}

Related

  1. writeAll(GatheringByteChannel ch, ByteBuffer... bbs)
  2. writeAllToChannel(List buffers, WritableByteChannel channel)
  3. writeBE(ByteBuffer bb, int elementWidth, long value)
  4. writeBER32(ByteBuffer buffer, int value)
  5. writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length)
  6. writeBooleanArray(boolean[] array, ByteBuffer out)
  7. writeBuffer(ByteBuffer dest, ByteBuffer src)
  8. writeBuffer(ByteChannel channel, ByteBuffer buffer)
  9. writeBuffer(FileChannel fc, ByteBuffer buf, int startPos)