List of usage examples for java.io RandomAccessFile length
public native long length() throws IOException;
From source file:com.piaoyou.util.FileUtil.java
public static String[] getLastLines(File file, int linesToReturn) throws IOException, FileNotFoundException { final int AVERAGE_CHARS_PER_LINE = 250; final int BYTES_PER_CHAR = 2; RandomAccessFile randomAccessFile = null; StringBuffer buffer = new StringBuffer(linesToReturn * AVERAGE_CHARS_PER_LINE); int lineTotal = 0; try {// w ww . ja v a 2 s . c o m randomAccessFile = new RandomAccessFile(file, "r"); long byteTotal = randomAccessFile.length(); long byteEstimateToRead = linesToReturn * AVERAGE_CHARS_PER_LINE * BYTES_PER_CHAR; long offset = byteTotal - byteEstimateToRead; if (offset < 0) { offset = 0; } randomAccessFile.seek(offset); //log.debug("SKIP IS ::" + offset); String line = null; String lineUTF8 = null; while ((line = randomAccessFile.readLine()) != null) { lineUTF8 = new String(line.getBytes("ISO8859_1"), "UTF-8"); lineTotal++; buffer.append(lineUTF8).append('\n'); } } finally { if (randomAccessFile != null) { try { randomAccessFile.close(); } catch (IOException ex) { } } } String[] resultLines = new String[linesToReturn]; BufferedReader in = null; try { in = new BufferedReader(new StringReader(buffer.toString())); int start = lineTotal /* + 2 */ - linesToReturn; // Ex : 55 - 10 = 45 ~ offset if (start < 0) start = 0; // not start line for (int i = 0; i < start; i++) { in.readLine(); // loop until the offset. Ex: loop 0, 1 ~~ 2 lines } int i = 0; String line = null; while ((line = in.readLine()) != null) { resultLines[i] = line; i++; } } catch (IOException ie) { log.error("Error" + ie); throw ie; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } } return resultLines; }
From source file:org.apache.jackrabbit.oak.segment.file.TarReader.java
/** * Tries to read an existing index from the given tar file. The index is * returned if it is found and looks valid (correct checksum, passes * sanity checks).//from ww w .j a v a 2 s. co m * * @param file tar file * @param name name of the tar file, for logging purposes * @return tar index, or {@code null} if not found or not valid * @throws IOException if the tar file could not be read */ private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException { long length = file.length(); if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) { log.warn("Unexpected size {} of tar file {}", length, name); return null; // unexpected file size } // read the index metadata just before the two final zero blocks ByteBuffer meta = ByteBuffer.allocate(16); file.seek(length - 2 * BLOCK_SIZE - 16); file.readFully(meta.array()); int crc32 = meta.getInt(); int count = meta.getInt(); int bytes = meta.getInt(); int magic = meta.getInt(); if (magic != INDEX_MAGIC) { return null; // magic byte mismatch } if (count < 1 || bytes < count * TarEntry.SIZE + 16 || bytes % BLOCK_SIZE != 0) { log.warn("Invalid index metadata in tar file {}", name); return null; // impossible entry and/or byte counts } // this involves seeking backwards in the file, which might not // perform well, but that's OK since we only do this once per file ByteBuffer index = ByteBuffer.allocate(count * TarEntry.SIZE); file.seek(length - 2 * BLOCK_SIZE - 16 - count * TarEntry.SIZE); file.readFully(index.array()); index.mark(); CRC32 checksum = new CRC32(); long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE; long lastmsb = Long.MIN_VALUE; long lastlsb = Long.MIN_VALUE; byte[] entry = new byte[TarEntry.SIZE]; for (int i = 0; i < count; i++) { index.get(entry); checksum.update(entry); ByteBuffer buffer = wrap(entry); long msb = buffer.getLong(); long lsb = buffer.getLong(); int offset = buffer.getInt(); int size = buffer.getInt(); if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) { log.warn("Incorrect index ordering in tar file {}", name); return null; } else if (lastmsb == msb && lastlsb == lsb && i > 0) { log.warn("Duplicate index entry in tar file {}", name); return null; } else if (offset < 0 || offset % BLOCK_SIZE != 0) { log.warn("Invalid index entry offset in tar file {}", name); return null; } else if (size < 1 || offset + size > limit) { log.warn("Invalid index entry size in tar file {}", name); return null; } lastmsb = msb; lastlsb = lsb; } if (crc32 != (int) checksum.getValue()) { log.warn("Invalid index checksum in tar file {}", name); return null; // checksum mismatch } index.reset(); return index; }
From source file:org.jevis.commons.JEVisFileImp.java
@Override public void loadFromFile(File file) throws IOException { RandomAccessFile f = new RandomAccessFile(file, "r"); byte[] bytes = new byte[(int) f.length()]; f.read(bytes);//w ww .jav a 2 s . c o m this.bytes = bytes; f.close(); }
From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java
/** * Scans through the tar file, looking for all segment entries. * * @throws IOException if the tar file could not be read *///from ww w. ja v a 2 s . co m private static void recoverEntries(File file, RandomAccessFile access, LinkedHashMap<UUID, byte[]> entries) throws IOException { byte[] header = new byte[BLOCK_SIZE]; while (access.getFilePointer() + BLOCK_SIZE <= access.length()) { // read the tar header block access.readFully(header); // compute the header checksum int sum = 0; for (int i = 0; i < BLOCK_SIZE; i++) { sum += header[i] & 0xff; } // identify possible zero block if (sum == 0 && access.getFilePointer() + 2 * BLOCK_SIZE == access.length()) { return; // found the zero blocks at the end of the file } // replace the actual stored checksum with spaces for comparison for (int i = 148; i < 148 + 8; i++) { sum -= header[i] & 0xff; sum += ' '; } byte[] checkbytes = String.format("%06o\0 ", sum).getBytes(UTF_8); for (int i = 0; i < checkbytes.length; i++) { if (checkbytes[i] != header[148 + i]) { log.warn("Invalid entry checksum at offset {} in tar file {}, skipping...", access.getFilePointer() - BLOCK_SIZE, file); } } // The header checksum passes, so read the entry name and size ByteBuffer buffer = ByteBuffer.wrap(header); String name = readString(buffer, 100); buffer.position(124); int size = readNumber(buffer, 12); if (access.getFilePointer() + size > access.length()) { // checksum was correct, so the size field should be accurate log.warn("Partial entry {} in tar file {}, ignoring...", name, file); return; } Matcher matcher = NAME_PATTERN.matcher(name); if (matcher.matches()) { UUID id = UUID.fromString(matcher.group(1)); String checksum = matcher.group(3); if (checksum != null || !entries.containsKey(id)) { byte[] data = new byte[size]; access.readFully(data); // skip possible padding to stay at block boundaries long position = access.getFilePointer(); long remainder = position % BLOCK_SIZE; if (remainder != 0) { access.seek(position + (BLOCK_SIZE - remainder)); } if (checksum != null) { CRC32 crc = new CRC32(); crc.update(data); if (crc.getValue() != Long.parseLong(checksum, 16)) { log.warn("Checksum mismatch in entry {} of tar file {}, skipping...", name, file); continue; } } entries.put(id, data); } } else if (!name.equals(file.getName() + ".idx")) { log.warn("Unexpected entry {} in tar file {}, skipping...", name, file); long position = access.getFilePointer() + size; long remainder = position % BLOCK_SIZE; if (remainder != 0) { position += BLOCK_SIZE - remainder; } access.seek(position); } } }
From source file:eu.trentorise.smartcampus.feedback.test.TestFeedbackManagers.java
private byte[] readTestFile() throws IOException { RandomAccessFile f = new RandomAccessFile("src/test/resources/android.jpg", "r"); byte[] b = new byte[(int) f.length()]; f.read(b);/* w w w . j a v a 2s . c om*/ f.close(); return b; }
From source file:ch.cyberduck.core.io.FileBuffer.java
@Override public synchronized int read(final byte[] chunk, final Long offset) throws IOException { final RandomAccessFile file = random(); if (offset < file.length()) { file.seek(offset);/*from www. j ava 2s . co m*/ if (chunk.length + offset > file.length()) { return file.read(chunk, 0, (int) (file.length() - offset)); } else { return file.read(chunk, 0, chunk.length); } } else { final NullInputStream nullStream = new NullInputStream(length); if (nullStream.available() > 0) { nullStream.skip(offset); return nullStream.read(chunk, 0, chunk.length); } else { return IOUtils.EOF; } } }
From source file:org.waveprotocol.box.server.persistence.file.DeltaStoreTest.java
public void testRecoverFromTruncatedDeltas() throws Exception { // Create an entry with one record. Shrink the file byte by byte and ensure // we can read it without crashing. DeltaStore store = newDeltaStore();// w w w . java 2 s. c om WaveletDeltaRecord written = createRecord(); File deltaFile = FileDeltaCollection.deltasFile(path.getAbsolutePath(), WAVE1_WAVELET1); long toRemove = 1; while (true) { // This generates the full file. DeltasAccess wavelet = store.open(WAVE1_WAVELET1); wavelet.append(ImmutableList.of(written)); wavelet.close(); RandomAccessFile file = new RandomAccessFile(deltaFile, "rw"); if (toRemove > file.length()) { file.close(); break; } // eat the planned number of bytes LOG.info("trying to remove " + toRemove + " bytes"); file.setLength(file.length() - toRemove); file.close(); wavelet = store.open(WAVE1_WAVELET1); WaveletDeltaRecord read = wavelet.getDelta(0); assertNull("got an unexpected record " + read, read); wavelet.close(); toRemove++; } }
From source file:ch.cyberduck.core.io.FileBuffer.java
@Override public void truncate(final Long length) { this.length = length; if (temporary.exists()) { try {/*w ww.jav a2 s . c o m*/ final RandomAccessFile file = random(); if (length < file.length()) { // Truncate current file.setLength(length); } } catch (IOException e) { log.warn(String.format("Failure truncating file %s to %d", temporary, length)); } } }
From source file:jp.andeb.obbutil.ObbUtilMain.java
private static boolean doRemove(String[] args) { if (args.length != 1) { printUsage(PROGNAME);//from w ww.j a va 2 s .co m return false; } final File targetFile = new File(args[0]); final RandomAccessFile targetRaFile; try { targetRaFile = new RandomAccessFile(targetFile, "rw"); } catch (FileNotFoundException e) { System.err.println("????: " + targetFile.getPath()); return false; } try { final ObbInfoV1 obbInfo; try { obbInfo = ObbInfoV1.fromFile(targetRaFile); } catch (IOException e) { System.err .println("????????: " + targetFile.getPath()); return false; } catch (NotObbException e) { System.err.println( "? OBB ???????: " + targetFile.getPath()); return false; } final ByteBuffer obbInfoBytes = obbInfo.toBytes(); targetRaFile.setLength(targetRaFile.length() - obbInfoBytes.remaining()); } catch (IOException e) { System.err.println("OBB ??????: " + targetFile.getPath()); return false; } finally { try { targetRaFile.close(); } catch (IOException e) { System.err.println("OBB ??????: " + targetFile.getPath()); return false; } } System.err.println("OBB ???????: " + targetFile.getPath()); return true; }
From source file:com.louding.frame.http.download.FileEntityHandler.java
public File handleEntity(HttpEntity entity, DownloadProgress callback, File save, boolean isResume) throws IOException { long current = 0; RandomAccessFile file = new RandomAccessFile(save, "rw"); if (isResume) { current = file.length(); }//from w ww . jav a 2 s. c om InputStream input = entity.getContent(); long count = entity.getContentLength() + current; if (mStop) { FileUtils.closeIO(file); return save; } // ??????? /** * <br> * current = input.skip(current); <br> * file.seek(current); <br> * ?JDKInputstream.skip(long i)i<br> * ? n ??????? */ file.seek(input.skip(current)); int readLen = 0; byte[] buffer = new byte[1024]; while ((readLen = input.read(buffer, 0, 1024)) != -1) { if (mStop) { break; } else { file.write(buffer, 0, readLen); current += readLen; callback.onProgress(count, current); } } callback.onProgress(count, current); if (mStop && current < count) { // ? FileUtils.closeIO(file); throw new IOException("user stop download thread"); } FileUtils.closeIO(file); return save; }