List of usage examples for java.io RandomAccessFile seek
public void seek(long pos) throws IOException
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 */// w w w . j av a 2s .co m 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 www . j ava2s . co m*/ 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:com.ettrema.zsync.UploadReader.java
/** * Reads a number of bytes from the InputStream equal to the size of the specified Range and * writes them into that Range of the RandomAccessFile. * /*www. ja v a2s . com*/ * @param dataIn The InputStream containing the data to be copied * @param range The target location in the RandomAccessFile * @param buffer A byte array used to transfer data from dataIn to fileOut * @param fileOut A RandomAccessFile for the File being assembled * @throws IOException */ private static void sendBytes(InputStream dataIn, Range range, byte[] buffer, RandomAccessFile fileOut) throws IOException { long bytesLeft = (range.getFinish() - range.getStart()); int bytesRead = 0; int readAtOnce = 0; fileOut.seek(range.getStart()); while (bytesLeft > 0) { readAtOnce = (int) Math.min(buffer.length, bytesLeft); bytesRead = dataIn.read(buffer, 0, readAtOnce); fileOut.write(buffer, 0, bytesRead); bytesLeft -= bytesRead; if (bytesLeft > 0 && bytesRead < 0) { throw new RuntimeException("Unable to copy byte Range: " + range.getRange() + ". End of InputStream reached with " + bytesLeft + " bytes left."); } } }
From source file:org.apache.hadoop.dfs.TestDatanodeBlockScanner.java
public static boolean corruptReplica(String blockName, int replica) throws IOException { Random random = new Random(); File baseDir = new File(System.getProperty("test.build.data"), "dfs/data"); boolean corrupted = false; for (int i = replica * 2; i < replica * 2 + 2; i++) { File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + blockName); if (blockFile.exists()) { // Corrupt replica by writing random bytes into replica RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw"); FileChannel channel = raFile.getChannel(); String badString = "BADBAD"; int rand = random.nextInt((int) channel.size() / 2); raFile.seek(rand); raFile.write(badString.getBytes()); raFile.close();/*from www . ja va 2s . co m*/ corrupted = true; } } return corrupted; }
From source file:dk.netarkivet.common.utils.cdx.BinSearch.java
/** Perform a binary search for a string in a file. * Returns the position of a line that begins with 'find'. * Note that this may not be the first line, if there be duplicates. * @param in the RandomAccessFile/* w w w .j a va 2 s . c o m*/ * @param find The String to look for in the above file * @throws IOException If some I/O error occurs * @return The index of a line matching find, or -1 if none found. */ private static long binSearch(RandomAccessFile in, String find) throws IOException { // The starting position for the binary search. Always // at the start of a line that's < the wanted line. long startpos = 0; // Ensure that startpos isn't a match. in.seek(startpos); String line = in.readLine(); if (line == null) { return -1; } if (compare(line, find) == 0) { return startpos; } // The ending position for the binary search. Always // *after* a line that >= the wanted line (which also means // at the start of a line that's > the wanted line, or at EOF long endpos = in.length(); // Set file pos to first line after middle. findMiddleLine(in, startpos, endpos); // When done searching, midpos points to a matching line, if any // Until the search is done, both endpos and startpos point // at non-matching lines (or EOF), and startpos < prevpos < endpos long prevpos = in.getFilePointer(); do { line = in.readLine(); if (line == null) { log.debug("Internal: Ran past end of file in '" + in + "' at " + endpos); return -1; } int cmp = compare(line, find); if (cmp > 0) { endpos = prevpos; } else if (cmp < 0) { startpos = prevpos; } else { return prevpos; } if (startpos == endpos) { return -1; } prevpos = findMiddleLine(in, startpos, endpos); if (prevpos == -1) { return -1; } } while (true); }
From source file:jsave.Utils.java
public static final void align_32(RandomAccessFile raf) throws IOException { long pos = raf.getFilePointer(); if (pos % 4 != 0) { raf.seek(pos + 4 - pos % 4); }/*from w ww .j ava2s . c o m*/ }
From source file:org.gcaldaemon.core.sendmail.SendMail.java
private static final String readContent(RandomAccessFile raf) throws Exception { String content = null;/* www.ja v a 2 s . c o m*/ int len = Math.min((int) raf.length(), 500); byte[] bytes = new byte[len]; raf.seek(0); raf.readFully(bytes); if (bytes[0] == -17 && bytes[1] == -69 && bytes[2] == -65) { // UTF-8 header found log.debug("File encoding is 'UTF-8'."); bytes = new byte[(int) raf.length() - 3]; raf.seek(3); raf.readFully(bytes); content = StringUtils.decodeToString(bytes, StringUtils.UTF_8).trim(); } else { // Autodetect encoding String header = StringUtils.decodeToString(bytes, StringUtils.US_ASCII).toUpperCase().replace(':', '='); String encoding = null; int pos = header.indexOf("ENCODING="); if (pos != -1) { int end = endOf(header, pos + 10); if (end != -1) { encoding = header.substring(pos + 9, end); encoding = encoding.replace('\'', ' '); encoding = encoding.replace('\"', ' '); encoding = encoding.trim(); } } // Check XML-encoding if (encoding == null) { pos = header.indexOf("CHARSET="); if (pos != -1) { int end = endOf(header, pos + 10); if (end != -1) { encoding = header.substring(pos + 8, end); encoding = encoding.replace('\'', ' '); encoding = encoding.replace('\"', ' '); encoding = encoding.trim(); } } } // Convert file encoding to Java encoding if (encoding == null) { encoding = Charset.defaultCharset().name(); } else { if (encoding.equals("UTF8")) { encoding = StringUtils.UTF_8; } } log.debug("File encoding is '" + encoding + "'."); bytes = new byte[(int) raf.length()]; raf.seek(0); raf.readFully(bytes); content = StringUtils.decodeToString(bytes, encoding).trim(); } return content; }
From source file:phex.util.FileUtils.java
/** * Splits the source file at the splitpoint into the destination file. * The result will be the source file containing data to the split point and * the destination file containing the data from the split point to the end * of the file.//from w w w .j a v a 2s . c om * * @param source the source file to split. * @param destination the destination file to split into. * @param splitPoint the split point byte position inside the file. * When the file size is 10 and the splitPoint is 3 the source file will contain * the first 3 bytes while the destination file will contain the last 7 bytes. * @throws IOException in case of a IOException during split operation. */ public static void splitFile(File source, File destination, long splitPoint) throws IOException { // open files RandomAccessFile sourceFile = new RandomAccessFile(source, "rws"); try { FileOutputStream outStream = new FileOutputStream(destination); try { sourceFile.seek(splitPoint); byte[] buffer = new byte[(int) Math.min(BUFFER_LENGTH, source.length() + 1)]; int length; while (-1 != (length = sourceFile.read(buffer))) { outStream.write(buffer, 0, length); } sourceFile.setLength(splitPoint); } finally { IOUtil.closeQuietly(outStream); } } finally { IOUtil.closeQuietly(sourceFile); } }
From source file:org.apache.storm.utils.ServerUtils.java
/** * Given a zip File input it will return its size * Only works for zip files whose uncompressed size is less than 4 GB, * otherwise returns the size module 2^32, per gzip specifications * @param myFile The zip file as input//from www .ja v a2 s .c o m * @throws IOException * @return zip file size as a long */ public static long zipFileSize(File myFile) throws IOException { RandomAccessFile raf = new RandomAccessFile(myFile, "r"); raf.seek(raf.length() - 4); long b4 = raf.read(); long b3 = raf.read(); long b2 = raf.read(); long b1 = raf.read(); long val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4; raf.close(); return val; }
From source file:eionet.eunis.servlets.DownloadServlet.java
/** * Copy the given byte range of the given input to the given output. * * @param input The input to copy the given range to the given output for. * @param output The output to copy the given range from the given input for. * @param start Start of the byte range. * @param length Length of the byte range. * @throws IOException If something fails at I/O level. */// w ww . ja v a 2s .c o m private static void copy(RandomAccessFile input, OutputStream output, long start, long length) throws IOException { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int read; if (input.length() == length) { // Write full range. while ((read = input.read(buffer)) > 0) { output.write(buffer, 0, read); } } else { // Write partial range. input.seek(start); long toRead = length; while ((read = input.read(buffer)) > 0) { toRead = toRead - read; if (toRead > 0) { output.write(buffer, 0, read); } else { output.write(buffer, 0, (int) toRead + read); break; } } } }