Here you can find the source of readBlock(ByteBuffer buffer, String fileName, long startOffset, Logger log)
Parameter | Description |
---|---|
buffer | Buffer ready for reading from position to limit. Need to set the limit before call. On successful return it has the position and limit ready for reading. |
fileName | a parameter |
startOffset | 0-based start position in file, inclusive. |
log | Optional. |
public static int readBlock(ByteBuffer buffer, String fileName, long startOffset, Logger log)
//package com.java2s; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.ReadableByteChannel; import java.util.logging.Level; import java.util.logging.Logger; public class Main { /**/* w w w. j a va 2s . c o m*/ * Read one block from data file. * * @param fileName * @param startOffset * 0-based start position in file, inclusive. * @param length * @param log * @return Null if failed to read form some reason. */ public static ByteBuffer readBlock(String fileName, long startOffset, int length, ByteOrder byteOrder, Logger log) { File file = new File(fileName); // // File is supposed to be on disk // if (!file.exists()) { if (log != null) log.log(Level.WARNING, "Cannot read file {0}, that does not exist", fileName); return null; } // // Make sure that position and length match the file size // if (startOffset + length > file.length()) { if (log != null) log.log(Level.SEVERE, "Wrong position and/or length when trying to read file \"" + fileName + "\", file length " + file.length() + ", offset " + startOffset + ", data length " + length); return null; } RandomAccessFile randomAccessFile; // // 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 read:\n" + e); return null; } // // 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 read file \"" + fileName + "\", file length " + file.length() + ", offset " + startOffset + ":\n" + e); try { randomAccessFile.close(); } catch (Exception e2) { } return null; } // Allocate new buffer ByteBuffer buffer = ByteBuffer.allocate(length).order(byteOrder); ReadableByteChannel channel = randomAccessFile.getChannel(); // // Read the data // int readBytes = 0; try { readBytes = channel.read(buffer); } catch (IOException e) { if (log != null) log.log(Level.SEVERE, "Failed to read data from \"" + fileName + "\", file length " + file.length() + ", offset " + startOffset + ", data length " + length + ":\n" + e); try { randomAccessFile.close(); } catch (Exception e2) { } return null; } // // Close the file // try { randomAccessFile.close(); } catch (Exception e2) { } // // Check if all bytes were written // if (readBytes != length) { if (log != null) log.log(Level.SEVERE, "Failed to read data from \"" + fileName + "\", read only " + readBytes + " instead of " + length); return null; } buffer.flip(); return buffer; } /** * Read one block from data file. * * @param buffer * Buffer ready for reading from position to limit. Need to set * the limit before call. On successful return it has the * position and limit ready for reading. * @param fileName * @param startOffset * 0-based start position in file, inclusive. * @param log * Optional. * @return Number of read bytes or 0 on error: missing file, end of file, * buffer is not large enough, etc. */ public static int readBlock(ByteBuffer buffer, String fileName, long startOffset, Logger log) { File file = new File(fileName); // // File is supposed to be on disk // if (!file.exists()) { if (log != null) log.log(Level.WARNING, "Cannot read file {0}, that does not exist", fileName); return 0; } // // Make sure that position and length match the file size // if (startOffset + buffer.remaining() > file.length()) { int length = (int) (file.length() - startOffset); if (length <= 0) { if (log != null) log.log(Level.SEVERE, "Wrong position and/or length when trying to read file \"" + fileName + "\", file length " + file.length() + ", offset " + startOffset + ", data length " + length); return 0; } buffer.limit(buffer.position() + length); } RandomAccessFile randomAccessFile; // // 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 read:\n" + e); return 0; } // // 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 read file \"" + fileName + "\", file length " + file.length() + ", offset " + startOffset + ":\n" + e); try { randomAccessFile.close(); } catch (Exception e2) { } return 0; } ReadableByteChannel channel = randomAccessFile.getChannel(); // // Read the data // int readBytes = 0; try { readBytes = channel.read(buffer); } catch (IOException e) { if (log != null) log.log(Level.SEVERE, "Failed to read data from \"" + fileName + "\", file length " + file.length() + ", offset " + startOffset + ", data length " + buffer.remaining() + ":\n" + e); try { randomAccessFile.close(); } catch (Exception e2) { } return 0; } // // Close the file // try { randomAccessFile.close(); } catch (Exception e2) { } buffer.limit(buffer.position()); buffer.position(buffer.position() - readBytes); return readBytes; } }