List of usage examples for java.io RandomAccessFile read
public int read(byte b[], int off, int len) throws IOException
From source file:Main.java
public static void main(String[] args) { try {/*from w w w. j ava 2s . com*/ byte[] b1 = { 1, 2, 3 }; byte[] b2 = { 1, 2, 3, 4, 5, 6, 7, 8 }; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("Hello World from java2s.com"); // set the file pointer at 0 position raf.seek(0); // read 2 bytes, starting from 1 System.out.println(raf.read(b1, 1, 2)); // set the file pointer at 0 position raf.seek(0); // read 3 bytes, starting from 4rth System.out.println(raf.read(b2, 4, 3)); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:shootersubdownloader.Shootersubdownloader.java
static String computefilehash(File f) throws Exception { if (!f.exists() || f.length() < 8 * 1024) { return null; }/*from w w w .ja v a 2 s . com*/ long l = f.length(); long[] offset = new long[4]; offset[3] = l - 8 * 1024; offset[2] = l / 3; offset[1] = l / 3 * 2; offset[0] = 4 * 1024; byte[] bBuf = new byte[1024 * 4]; RandomAccessFile raf = new RandomAccessFile(f, "r"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; i++) { raf.seek(offset[i]); int readlen = raf.read(bBuf, 0, 4 * 1024); String md5 = md5(bBuf); if (sb.length() != 0) { sb.append("%3B"); } sb.append(md5); } raf.close(); return sb.toString(); }
From source file:com.netflix.imfutility.itunes.image.ImageValidator.java
private static boolean checkBytes(File file, byte[] start, byte[] end) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); byte[] destStart = new byte[start.length]; if (start.length != 0) { int res = randomAccessFile.read(destStart, 0, start.length); if (res == -1) { return false; }//from w w w. jav a 2s . c o m } byte[] destEnd = new byte[end.length]; if (end.length != 0) { randomAccessFile.seek(file.length() - end.length); int res = randomAccessFile.read(destEnd, 0, end.length); if (res == -1) { return false; } } return Arrays.equals(start, destStart) && Arrays.equals(end, destEnd); }
From source file:org.jcodec.common.io.Buffer.java
private static int read(byte[] buffer, int pos, int size, RandomAccessFile is) throws IOException { int read, total = 0; while (size > 0 && (read = is.read(buffer, pos, size)) != -1) { pos += read;//from w w w . ja va 2s. c o m size -= read; total += read; } return total; }
From source file:org.piwigo.remotesync.api.util.FileUtil.java
/** * chunkNumber start at 0//from w ww . j a v a2s . c o m */ public static byte[] getFilePart(File file, int chunkSize, int chunkNumber) { RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile(file, "r"); int bytesSize = getChunkSize(file, chunkSize, chunkNumber); byte[] bytes = new byte[bytesSize]; randomAccessFile.seek(chunkSize * chunkNumber); randomAccessFile.read(bytes, 0, bytesSize); return bytes; } catch (IOException e) { throw new IORuntimeException("Cannot get file part", e); } finally { IOUtils.closeQuietly(randomAccessFile); } }
From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java
private static byte[] getTailBytes(File logFile) throws FileNotFoundException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = null; int len;//ww w .ja va 2 s . c om int size = 1024; try { RandomAccessFile randomAccessFile = new RandomAccessFile(logFile, "r"); long lengthFile = randomAccessFile.length(); if (lengthFile >= LOG_TAIL_FOR_PACKING_SIZE) { randomAccessFile.seek(lengthFile - LOG_TAIL_FOR_PACKING_SIZE); } buf = new byte[size]; while ((len = randomAccessFile.read(buf, 0, size)) != -1) { bos.write(buf, 0, len); } buf = bos.toByteArray(); } catch (IOException e) { log.error("Unable to get tail for log file " + logFile.getName(), e); } finally { IOUtils.closeQuietly(bos); } return buf; }
From source file:com.aoyetech.fee.commons.utils.FileUtils.java
public static String getFileContent(final File tFile) throws IOException, FileNotFoundException, UnsupportedEncodingException { if (!tFile.isFile()) { throw new IOException("?"); }//from ww w . j av a2 s .co m final RandomAccessFile file = new RandomAccessFile(tFile, "r"); final long fileSize = file.length(); final byte[] bytes = new byte[(int) fileSize]; long readLength = 0L; while (readLength < fileSize) { final int onceLength = file.read(bytes, (int) readLength, (int) (fileSize - readLength)); if (onceLength > 0) { readLength += onceLength; } else { break; } } IOUtils.closeQuietly(file); return new String(bytes, Constants.ENCODE); }
From source file:org.broadinstitute.gatk.utils.io.IOUtils.java
/** * Determines the uncompressed size of a GZIP file. Uses the GZIP ISIZE field in the last * 4 bytes of the file to get this information. * * @param gzipFile GZIP-format file whose uncompressed size to determine * @return The uncompressed size (in bytes) of the GZIP file *//*from w w w . j a v a2s. com*/ public static int getGZIPFileUncompressedSize(File gzipFile) { if (gzipFile == null) { throw new ReviewedGATKException("GZIP file to examine was null"); } try { // The GZIP ISIZE field holds the uncompressed size of the compressed data. // It occupies the last 4 bytes of any GZIP file: RandomAccessFile in = new RandomAccessFile(gzipFile, "r"); in.seek(gzipFile.length() - 4); byte[] sizeBytes = new byte[4]; in.read(sizeBytes, 0, 4); ByteBuffer byteBuf = ByteBuffer.wrap(sizeBytes); byteBuf.order(ByteOrder.LITTLE_ENDIAN); // The GZIP spec mandates little-endian byte order int uncompressedSize = byteBuf.getInt(); // If the size read in is negative, we've overflowed our signed integer: if (uncompressedSize < 0) { throw new UserException.CouldNotReadInputFile(String.format( "Cannot accurately determine the uncompressed size of file %s " + "because it's either larger than %d bytes or the GZIP ISIZE field is corrupt", gzipFile.getAbsolutePath(), Integer.MAX_VALUE)); } return uncompressedSize; } catch (IOException e) { throw new UserException.CouldNotReadInputFile(gzipFile, e); } }
From source file:org.broadinstitute.sting.utils.io.IOUtils.java
/** * Determines the uncompressed size of a GZIP file. Uses the GZIP ISIZE field in the last * 4 bytes of the file to get this information. * * @param gzipFile GZIP-format file whose uncompressed size to determine * @return The uncompressed size (in bytes) of the GZIP file *///from w w w . j a v a2 s .com public static int getGZIPFileUncompressedSize(File gzipFile) { if (gzipFile == null) { throw new ReviewedStingException("GZIP file to examine was null"); } try { // The GZIP ISIZE field holds the uncompressed size of the compressed data. // It occupies the last 4 bytes of any GZIP file: RandomAccessFile in = new RandomAccessFile(gzipFile, "r"); in.seek(gzipFile.length() - 4); byte[] sizeBytes = new byte[4]; in.read(sizeBytes, 0, 4); ByteBuffer byteBuf = ByteBuffer.wrap(sizeBytes); byteBuf.order(ByteOrder.LITTLE_ENDIAN); // The GZIP spec mandates little-endian byte order int uncompressedSize = byteBuf.getInt(); // If the size read in is negative, we've overflowed our signed integer: if (uncompressedSize < 0) { throw new UserException.CouldNotReadInputFile(String.format( "Cannot accurately determine the uncompressed size of file %s " + "because it's either larger than %d bytes or the GZIP ISIZE field is corrupt", gzipFile.getAbsolutePath(), Integer.MAX_VALUE)); } return uncompressedSize; } catch (IOException e) { throw new UserException.CouldNotReadInputFile(gzipFile, e); } }
From source file:au.org.ala.layers.grid.GridCacheBuilder.java
static void nextRowOfFloats(float[] row, String datatype, boolean byteOrderLSB, int ncols, RandomAccessFile raf, byte[] b, float noDataValue) throws IOException { int size = 4; if (datatype.charAt(0) == 'U') { size = 1;//from w w w. ja v a 2 s. c o m } else if (datatype.charAt(0) == 'B') { size = 1; } else if (datatype.charAt(0) == 'S') { size = 2; } else if (datatype.charAt(0) == 'I') { size = 4; } else if (datatype.charAt(0) == 'L') { size = 8; } else if (datatype.charAt(0) == 'F') { size = 4; } else if (datatype.charAt(0) == 'D') { size = 8; } raf.read(b, 0, size * ncols); ByteBuffer bb = ByteBuffer.wrap(b); if (byteOrderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } else { bb.order(ByteOrder.BIG_ENDIAN); } int i; int length = ncols; if (datatype.charAt(0) == 'U') { for (i = 0; i < length; i++) { float ret = bb.get(); if (ret < 0) { ret += 256; } row[i] = ret; } } else if (datatype.charAt(0) == 'B') { for (i = 0; i < length; i++) { row[i] = (float) bb.get(); } } else if (datatype.charAt(0) == 'S') { for (i = 0; i < length; i++) { row[i] = (float) bb.getShort(); } } else if (datatype.charAt(0) == 'I') { for (i = 0; i < length; i++) { row[i] = (float) bb.getInt(); } } else if (datatype.charAt(0) == 'L') { for (i = 0; i < length; i++) { row[i] = (float) bb.getLong(); } } else if (datatype.charAt(0) == 'F') { for (i = 0; i < length; i++) { row[i] = (float) bb.getFloat(); } } else if (datatype.charAt(0) == 'D') { for (i = 0; i < length; i++) { row[i] = (float) bb.getDouble(); } } else { logger.info("UNKNOWN TYPE: " + datatype); } for (i = 0; i < length; i++) { if (row[i] == noDataValue) { row[i] = Float.NaN; } } }