Java Code Examples for java.io.RandomAccessFile#readFully()
The following examples show how to use
java.io.RandomAccessFile#readFully() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UUIDUtil.java From AndroidPNClient with Apache License 2.0 | 7 votes |
/** * 将表示此设备在该程序上的唯一标识符写入程序文件系统中。 * * @param installation 保存唯一标识符的File对象。 * @return 唯一标识符。 * @throws java.io.IOException IO异常。 */ private static String readInstallationFile(File installation) throws IOException { RandomAccessFile accessFile = new RandomAccessFile(installation, "r"); byte[] bs = new byte[(int) accessFile.length()]; accessFile.readFully(bs); accessFile.close(); return new String(bs); }
Example 2
Source File: SpillableMapUtils.java From hudi with Apache License 2.0 | 6 votes |
/** * Reads the given file with specific pattern(|crc|timestamp|sizeOfKey|SizeOfValue|key|value|) then * returns an instance of {@link FileEntry}. */ private static FileEntry readInternal(RandomAccessFile file, long valuePosition, int valueLength) throws IOException { file.seek(valuePosition); long crc = file.readLong(); long timestamp = file.readLong(); int keySize = file.readInt(); int valueSize = file.readInt(); byte[] key = new byte[keySize]; file.readFully(key, 0, keySize); byte[] value = new byte[valueSize]; if (valueSize != valueLength) { throw new HoodieCorruptedDataException("unequal size of payload written to external file, data may be corrupted"); } file.readFully(value, 0, valueSize); long crcOfReadValue = generateChecksum(value); if (crc != crcOfReadValue) { throw new HoodieCorruptedDataException( "checksum of payload written to external disk does not match, data may be corrupted"); } return new FileEntry(crc, keySize, valueSize, key, value, timestamp); }
Example 3
Source File: ImageUtilities.java From geopaparazzi with GNU General Public License v3.0 | 6 votes |
/** * Get an image and thumbnail from a file by its path. * * @param imageFilePath the image path. * @param tryCount times to try in 300 millis loop, in case the image is * not yet on disk. (ugly but no other way right now) * @return the image and thumbnail data or null. */ public static byte[][] getImageAndThumbnailFromPath(String imageFilePath, int tryCount) throws IOException { byte[][] imageAndThumbNail = new byte[2][]; RandomAccessFile f = new RandomAccessFile(imageFilePath, "r"); byte[] imageByteArray = new byte[(int) f.length()]; f.readFully(imageByteArray); // first read full image and check existence Bitmap image = BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); // int count = 0; // while (image == null && ++count < tryCount) { // try { // Thread.sleep(300); // } catch (InterruptedException e) { // e.printStackTrace(); // } // image = BitmapFactory.decodeFile(imageFilePath); // } // if (image == null) return null; // It is necessary to rotate the image before converting to bytes, as the exif information // will be lost afterwards and the image will be incorrectly oriented in some devices float orientation = getRotation(imageFilePath); if (orientation > 0) { Matrix matrix = new Matrix(); matrix.postRotate(orientation); image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true); } int width = image.getWidth(); int height = image.getHeight(); // define sampling for thumbnail float sampleSizeF = (float) width / (float) THUMBNAILWIDTH; float newHeight = height / sampleSizeF; Bitmap thumbnail = Bitmap.createScaledBitmap(image, THUMBNAILWIDTH, (int) newHeight, false); ByteArrayOutputStream stream = new ByteArrayOutputStream(); thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, stream); byte[] thumbnailBytes = stream.toByteArray(); imageAndThumbNail[0] = imageByteArray; imageAndThumbNail[1] = thumbnailBytes; return imageAndThumbNail; }
Example 4
Source File: RAMBTreeFile.java From birt with Eclipse Public License 1.0 | 6 votes |
public void read( String file ) throws IOException { blocks.clear( ); RandomAccessFile rf = new RandomAccessFile( file, "r" ); try { int blockCount = (int) ( rf.length( ) / BLOCK_SIZE ); byte[] block = new byte[BLOCK_SIZE]; for ( int i = 0; i < blockCount; i++ ) { rf.readFully( block ); blocks.add( block ); } } finally { rf.close( ); } }
Example 5
Source File: CryptoUtils.java From kotlogram with MIT License | 6 votes |
public static String MD5(RandomAccessFile randomAccessFile) { try { MessageDigest crypt = md5.get(); crypt.reset(); byte[] block = new byte[8 * 1024]; for (int i = 0; i < randomAccessFile.length(); i += 8 * 1024) { int len = (int) Math.min(block.length, randomAccessFile.length() - i); randomAccessFile.readFully(block, 0, len); crypt.update(block, 0, len); } return ToHex(crypt.digest()); } catch (IOException e) { e.printStackTrace(); } return null; }
Example 6
Source File: VLCUtil.java From VCL-Android with Apache License 2.0 | 6 votes |
private static boolean readSection(RandomAccessFile in, ElfData elf) throws IOException { byte[] bytes = new byte[SECTION_HEADER_SIZE]; in.seek(elf.e_shoff); for (int i = 0; i < elf.e_shnum; ++i) { in.readFully(bytes); // wrap bytes in a ByteBuffer to force endianess ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(elf.order); int sh_type = buffer.getInt(4); /* Section type */ if (sh_type != SHT_ARM_ATTRIBUTES) continue; elf.sh_offset = buffer.getInt(16); /* Section file offset */ elf.sh_size = buffer.getInt(20); /* Section size in bytes */ return true; } return false; }
Example 7
Source File: ColonizationMapLoader.java From freecol with GNU General Public License v2.0 | 6 votes |
public ColonizationMapLoader(File file) throws IOException { try { RandomAccessFile reader = new RandomAccessFile(file, "r"); reader.readFully(header); int size = header[WIDTH] * header[HEIGHT]; layer1 = new byte[size]; reader.readFully(layer1); } catch (EOFException ee) { logger.log(Level.SEVERE, "File (" + file + ") is too short.", ee); } catch (FileNotFoundException fe) { logger.log(Level.SEVERE, "File (" + file + ") was not found.", fe); } catch (IOException e) { logger.log(Level.SEVERE, "File (" + file + ") is corrupt and cannot be read.", e); } }
Example 8
Source File: VBRTag.java From java-lame with GNU Lesser General Public License v3.0 | 6 votes |
private int skipId3v2(final RandomAccessFile fpStream) throws IOException { // seek to the beginning of the stream fpStream.seek(0); // read 10 bytes in case there's an ID3 version 2 header here byte[] id3v2Header = new byte[10]; fpStream.readFully(id3v2Header); /* does the stream begin with the ID3 version 2 file identifier? */ int id3v2TagSize; if (!new String(id3v2Header, "ISO-8859-1").startsWith("ID3")) { /* * the tag size (minus the 10-byte header) is encoded into four * bytes where the most significant bit is clear in each byte */ id3v2TagSize = (((id3v2Header[6] & 0x7f) << 21) | ((id3v2Header[7] & 0x7f) << 14) | ((id3v2Header[8] & 0x7f) << 7) | (id3v2Header[9] & 0x7f)) + id3v2Header.length; } else { /* no ID3 version 2 tag in this stream */ id3v2TagSize = 0; } return id3v2TagSize; }
Example 9
Source File: CryptoUtils.java From TelegramApi with MIT License | 6 votes |
public static String MD5(File file) { try { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); MessageDigest crypt = md5.get(); crypt.reset(); byte[] block = new byte[8 * 1024]; for (int i = 0; i < randomAccessFile.length(); i += 8 * 1024) { int len = (int) Math.min(block.length, randomAccessFile.length() - i); randomAccessFile.readFully(block, 0, len); crypt.update(block, 0, len); } return ToHex(crypt.digest()); } catch (IOException e) { e.printStackTrace(); } return null; }
Example 10
Source File: ApkExternalInfoTool.java From letv with Apache License 2.0 | 5 votes |
private static byte[] a(RandomAccessFile randomAccessFile) throws IOException { int i = 1; long length = randomAccessFile.length() - 22; randomAccessFile.seek(length); byte[] bytes = a.getBytes(); byte read = randomAccessFile.read(); while (read != (byte) -1) { if (read == bytes[0] && randomAccessFile.read() == bytes[1] && randomAccessFile.read() == bytes[2] && randomAccessFile.read() == bytes[3]) { break; } length--; randomAccessFile.seek(length); read = randomAccessFile.read(); } i = 0; if (i == 0) { throw new ZipException("archive is not a ZIP archive"); } randomAccessFile.seek((16 + length) + 4); byte[] bArr = new byte[2]; randomAccessFile.readFully(bArr); i = new ZipShort(bArr).getValue(); if (i == 0) { return null; } bArr = new byte[i]; randomAccessFile.read(bArr); return bArr; }
Example 11
Source File: RecordingInput.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public void read(RandomAccessFile file, int amount) throws IOException { blockPosition = file.getFilePointer(); // reuse byte array, if possible if (amount != bytes.length) { bytes = new byte[amount]; } file.readFully(bytes); }
Example 12
Source File: TestCollectorE2EFailures.java From verigreen with Apache License 2.0 | 5 votes |
/** * @param pathToFile * @throws FileNotFoundException * @throws IOException */ private void writeToLocalFileInBeginning(String pathToFile, String content) throws FileNotFoundException, IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(new File(pathToFile), "rw"); byte[] bytes = new byte[(int) randomAccessFile.length()]; randomAccessFile.readFully(bytes); randomAccessFile.seek(0); randomAccessFile.write(content.getBytes()); randomAccessFile.write("\n".getBytes()); randomAccessFile.write(bytes); randomAccessFile.close(); }
Example 13
Source File: VLCUtil.java From OTTLivePlayer_vlc with MIT License | 5 votes |
private static boolean readHeader(RandomAccessFile in, ElfData elf) throws IOException { // http://www.sco.com/developers/gabi/1998-04-29/ch4.eheader.html byte[] bytes = new byte[ELF_HEADER_SIZE]; in.readFully(bytes); if (bytes[0] != 127 || bytes[1] != 'E' || bytes[2] != 'L' || bytes[3] != 'F' || (bytes[4] != 1 && bytes[4] != 2)) { Log.e(TAG, "ELF header invalid"); return false; } elf.is64bits = bytes[4] == 2; elf.order = bytes[5] == 1 ? ByteOrder.LITTLE_ENDIAN // ELFDATA2LSB : ByteOrder.BIG_ENDIAN; // ELFDATA2MSB // wrap bytes in a ByteBuffer to force endianess ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(elf.order); elf.e_machine = buffer.getShort(18); /* Architecture */ elf.e_shoff = buffer.getInt(32); /* Section header table file offset */ elf.e_shnum = buffer.getShort(48); /* Section header table entry count */ return true; }
Example 14
Source File: LogFile.java From mt-flume with Apache License 2.0 | 5 votes |
protected static byte[] readDelimitedBuffer(RandomAccessFile fileHandle) throws IOException { int length = fileHandle.readInt(); Preconditions.checkState(length >= 0, Integer.toHexString(length)); byte[] buffer = new byte[length]; fileHandle.readFully(buffer); return buffer; }
Example 15
Source File: Installation.java From javaide with GNU General Public License v3.0 | 5 votes |
private static String readInstallationFile(File installation) throws IOException { RandomAccessFile f = new RandomAccessFile(installation, "r"); byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes); }
Example 16
Source File: DeviceUidGenerator.java From Ticket-Analysis with MIT License | 5 votes |
private static String readInstallationFile(File installation) throws IOException { RandomAccessFile f = new RandomAccessFile(installation, "r"); byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes); }
Example 17
Source File: VLCUtil.java From OTTLivePlayer_vlc with MIT License | 4 votes |
private static boolean readArmAttributes(RandomAccessFile in, ElfData elf) throws IOException { byte[] bytes = new byte[elf.sh_size]; in.seek(elf.sh_offset); in.readFully(bytes); // wrap bytes in a ByteBuffer to force endianess ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(elf.order); //http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044e/IHI0044E_aaelf.pdf //http://infocenter.arm.com/help/topic/com.arm.doc.ihi0045d/IHI0045D_ABI_addenda.pdf if (buffer.get() != 'A') // format-version return false; // sub-sections loop while (buffer.remaining() > 0) { int start_section = buffer.position(); int length = buffer.getInt(); String vendor = getString(buffer); if (vendor.equals("aeabi")) { // tags loop while (buffer.position() < start_section + length) { int start = buffer.position(); int tag = buffer.get(); int size = buffer.getInt(); // skip if not Tag_File, we don't care about others if (tag != 1) { buffer.position(start + size); continue; } // attributes loop while (buffer.position() < start + size) { tag = getUleb128(buffer); if (tag == 6) { // CPU_arch int arch = getUleb128(buffer); elf.att_arch = CPU_archs[arch]; } else if (tag == 27) { // ABI_HardFP_use getUleb128(buffer); elf.att_fpu = true; } else { // string for 4=CPU_raw_name / 5=CPU_name / 32=compatibility // string for >32 && odd tags // uleb128 for other tag %= 128; if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0)) getString(buffer); else getUleb128(buffer); } } } break; } } return true; }
Example 18
Source File: HeaderReader.java From zip4j with Apache License 2.0 | 4 votes |
private Zip64EndOfCentralDirectoryRecord readZip64EndCentralDirRec(RandomAccessFile zip4jRaf, RawIO rawIO) throws IOException { if (zipModel.getZip64EndOfCentralDirectoryLocator() == null) { throw new ZipException("invalid zip64 end of central directory locator"); } long offSetStartOfZip64CentralDir = zipModel.getZip64EndOfCentralDirectoryLocator() .getOffsetZip64EndOfCentralDirectoryRecord(); if (offSetStartOfZip64CentralDir < 0) { throw new ZipException("invalid offset for start of end of central directory record"); } zip4jRaf.seek(offSetStartOfZip64CentralDir); Zip64EndOfCentralDirectoryRecord zip64EndOfCentralDirectoryRecord = new Zip64EndOfCentralDirectoryRecord(); int signature = rawIO.readIntLittleEndian(zip4jRaf); if (signature != HeaderSignature.ZIP64_END_CENTRAL_DIRECTORY_RECORD.getValue()) { throw new ZipException("invalid signature for zip64 end of central directory record"); } zip64EndOfCentralDirectoryRecord.setSignature(HeaderSignature.ZIP64_END_CENTRAL_DIRECTORY_RECORD); zip64EndOfCentralDirectoryRecord.setSizeOfZip64EndCentralDirectoryRecord(rawIO.readLongLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setVersionMadeBy(rawIO.readShortLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setVersionNeededToExtract(rawIO.readShortLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setNumberOfThisDisk(rawIO.readIntLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setNumberOfThisDiskStartOfCentralDirectory(rawIO.readIntLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setTotalNumberOfEntriesInCentralDirectoryOnThisDisk( rawIO.readLongLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setTotalNumberOfEntriesInCentralDirectory(rawIO.readLongLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setSizeOfCentralDirectory(rawIO.readLongLittleEndian(zip4jRaf)); zip64EndOfCentralDirectoryRecord.setOffsetStartCentralDirectoryWRTStartDiskNumber( rawIO.readLongLittleEndian(zip4jRaf)); //zip64 extensible data sector //44 is the size of fixed variables in this record long extDataSecSize = zip64EndOfCentralDirectoryRecord.getSizeOfZip64EndCentralDirectoryRecord() - 44; if (extDataSecSize > 0) { byte[] extDataSecRecBuf = new byte[(int) extDataSecSize]; zip4jRaf.readFully(extDataSecRecBuf); zip64EndOfCentralDirectoryRecord.setExtensibleDataSector(extDataSecRecBuf); } return zip64EndOfCentralDirectoryRecord; }
Example 19
Source File: LibVlcUtil.java From VLC-Simple-Player-Android with MIT License | 4 votes |
private static boolean readArmAttributes(RandomAccessFile in, ElfData elf) throws IOException { byte[] bytes = new byte[elf.sh_size]; in.seek(elf.sh_offset); in.readFully(bytes); // wrap bytes in a ByteBuffer to force endianess ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(elf.order); //http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044e/IHI0044E_aaelf.pdf //http://infocenter.arm.com/help/topic/com.arm.doc.ihi0045d/IHI0045D_ABI_addenda.pdf if (buffer.get() != 'A') // format-version return false; // sub-sections loop while (buffer.remaining() > 0) { int start_section = buffer.position(); int length = buffer.getInt(); String vendor = getString(buffer); if (vendor.equals("aeabi")) { // tags loop while (buffer.position() < start_section + length) { int start = buffer.position(); int tag = buffer.get(); int size = buffer.getInt(); // skip if not Tag_File, we don't care about others if (tag != 1) { buffer.position(start + size); continue; } // attributes loop while (buffer.position() < start + size) { tag = getUleb128(buffer); if (tag == 6) { // CPU_arch int arch = getUleb128(buffer); elf.att_arch = CPU_archs[arch]; } else if (tag == 27) { // ABI_HardFP_use getUleb128(buffer); elf.att_fpu = true; } else { // string for 4=CPU_raw_name / 5=CPU_name / 32=compatibility // string for >32 && odd tags // uleb128 for other tag %= 128; if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0)) getString(buffer); else getUleb128(buffer); } } } break; } } return true; }
Example 20
Source File: ZipUtils.java From titan-hotfix with Apache License 2.0 | 4 votes |
/** * Returns the ZIP End of Central Directory record of the provided ZIP file. * * @param maxCommentSize maximum accepted size (in bytes) of EoCD comment field. The permitted * value is from 0 to 65535 inclusive. The smaller the value, the faster this method * locates the record, provided its comment field is no longer than this value. * * @return contents of the ZIP End of Central Directory record and the record's offset in the * file or {@code null} if the file does not contain the record. * * @throws IOException if an I/O error occurs while reading the file. */ private static Pair<ByteBuffer, Long> findZipEndOfCentralDirectoryRecord( RandomAccessFile zip, int maxCommentSize) throws IOException { // ZIP End of Central Directory (EOCD) record is located at the very end of the ZIP archive. // The record can be identified by its 4-byte signature/magic which is located at the very // beginning of the record. A complication is that the record is variable-length because of // the comment field. // The algorithm for locating the ZIP EOCD record is as follows. We search backwards from // end of the buffer for the EOCD record signature. Whenever we find a signature, we check // the candidate record's comment length is such that the remainder of the record takes up // exactly the remaining bytes in the buffer. The search is bounded because the maximum // size of the comment field is 65535 bytes because the field is an unsigned 16-bit number. if ((maxCommentSize < 0) || (maxCommentSize > UINT16_MAX_VALUE)) { throw new IllegalArgumentException("maxCommentSize: " + maxCommentSize); } long fileSize = zip.length(); if (fileSize < ZIP_EOCD_REC_MIN_SIZE) { // No space for EoCD record in the file. return null; } // Lower maxCommentSize if the file is too small. maxCommentSize = (int) Math.min(maxCommentSize, fileSize - ZIP_EOCD_REC_MIN_SIZE); ByteBuffer buf = ByteBuffer.allocate(ZIP_EOCD_REC_MIN_SIZE + maxCommentSize); buf.order(ByteOrder.LITTLE_ENDIAN); long bufOffsetInFile = fileSize - buf.capacity(); zip.seek(bufOffsetInFile); zip.readFully(buf.array(), buf.arrayOffset(), buf.capacity()); int eocdOffsetInBuf = findZipEndOfCentralDirectoryRecord(buf); if (eocdOffsetInBuf == -1) { // No EoCD record found in the buffer return null; } // EoCD found buf.position(eocdOffsetInBuf); ByteBuffer eocd = buf.slice(); eocd.order(ByteOrder.LITTLE_ENDIAN); return Pair.create(eocd, bufOffsetInFile + eocdOffsetInBuf); }