Here you can find the source of writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log)
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 |
public static boolean writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log)
//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; } }