/** * 给定一个ip国家地区记录的偏移,返回一个IPLocation结构 * * @param offset 国家记录的起始偏移 * @return IPLocation对象 */ private IPLocation getIPLocation(long offset) { try { // 跳过4字节ip ipFile.seek(offset + 4); // 读取第一个字节判断是否标志字节 byte b = ipFile.readByte(); if (b == REDIRECT_MODE_1) { // 读取国家偏移 long countryOffset = readLong3(); // 跳转至偏移处 ipFile.seek(countryOffset); // 再检查一次标志字节,因为这个时候这个地方仍然可能是个重定向 b = ipFile.readByte(); if (b == REDIRECT_MODE_2) { loc.setCountry(readString(readLong3())); ipFile.seek(countryOffset + 4); } else loc.setCountry(readString(countryOffset)); // 读取地区标志 loc.setArea(readArea(ipFile.getFilePointer())); } else if (b == REDIRECT_MODE_2) { loc.setCountry(readString(readLong3())); loc.setArea(readArea(offset + 8)); } else { loc.setCountry(readString(ipFile.getFilePointer() - 1)); loc.setArea(readArea(ipFile.getFilePointer())); } return loc; } catch (IOException e) { return null; } }
public static long readUInteger(RandomAccessFile f) throws IOException { byte b1 = f.readByte(); byte b2 = f.readByte(); byte b3 = f.readByte(); byte b4 = f.readByte(); int number = ((b4 & 0xff) << 24) | ((b3 & 0xff) << 16) | ((b2 & 0xff) << 8) | ((b1 & 0xff) << 0); return (number & 0x00000000ffffffffL); }
/** * 从offset偏移处读取一个以0结束的字符串 * * @param offset * @return 读取的字符串,出错返回空字符串 */ private String readString(long offset) { try { ipFile.seek(offset); int i; for (i = 0, buf[i] = ipFile.readByte(); buf[i] != 0; buf[++i] = ipFile.readByte()) ; if (i != 0) return Utils.getString(buf, 0, i, "GBK"); } catch (IOException e) { System.out.println(e.getMessage()); } return ""; }
/** * 从offset偏移处读取一个以0结束的字符串 * * @param offset 字符串起始偏移 * @return 读取的字符串,出错返回空字符串 */ private String readString(long offset) { try { ipFile.seek(offset); int i; for (i = 0, buf[i] = ipFile.readByte(); buf[i] != 0; buf[++i] = ipFile.readByte()) ; if (i != 0) return Util.getString(buf, 0, i, "GBK"); } catch (IOException e) { LogFactory.log("", Level.ERROR, e); } return ""; }
/** * 从offset位置读取4个字节为一个long,因为java为big-endian格式,所以没办法 用了这么一个函数来做转换 * * @param offset * @return 读取的long值,返回-1表示读取文件失败 */ private long readLong4(long offset) { long ret = 0; try { ipFile.seek(offset); ret |= (ipFile.readByte() & 0xFF); ret |= ((ipFile.readByte() << 8) & 0xFF00); ret |= ((ipFile.readByte() << 16) & 0xFF0000); ret |= ((ipFile.readByte() << 24) & 0xFF000000); return ret; } catch (IOException e) { return -1; } }
private void update() throws DatabaseCorruptedException, IOException { try (RandomAccessFile file = new RandomAccessFile(filePath.toString(), "r")) { if (file.length() == 0) { throw new DatabaseCorruptedException("Data base corrupted: empty file found"); } List<String> keys = new LinkedList<>(); List<Integer> offsets = new LinkedList<>(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); byte b; int counter = 0; do { // Read keys. while ((b = file.readByte()) != 0) { counter++; bytes.write(b); } ++counter; offsets.add(file.readInt()); counter += 4; String key = bytes.toString(TableManager.CODE_FORMAT); bytes.reset(); if (!checkKey(key)) { throw new DatabaseCorruptedException("Wrong key found in file " + filePath.toString()); } keys.add(key); } while (counter < offsets.get(0)); offsets.add((int) file.length()); offsets.remove(0); // It's current position in file, we don't need it in list. Iterator<String> keyIterator = keys.iterator(); for (int nextOffset : offsets) { // Read values. while (counter < nextOffset) { bytes.write(file.readByte()); counter++; } if (bytes.size() > 0) { try { fileMap.put( keyIterator.next(), provider.deserialize(table, bytes.toString(TableManager.CODE_FORMAT))); } catch (ParseException e) { throw new RuntimeException( "Data corrupted in file " + filePath.toString() + " : " + e.getMessage()); } bytes.reset(); } else { throw new DatabaseCorruptedException("Data corrupted in file " + filePath.toString()); } } bytes.close(); } }
private boolean findSPSandPPS() { /* * SPS and PPS parameters are stored in the avcC box * You may find really useful information about this box * in the document ISO-IEC 14496-15, part 5.2.4.1.1 * The box's structure is described there * * aligned(8) class AVCDecoderConfigurationRecord { * unsigned int(8) configurationVersion = 1; * unsigned int(8) AVCProfileIndication; * unsigned int(8) profile_compatibility; * unsigned int(8) AVCLevelIndication; * bit(6) reserved = ‘111111’b; * unsigned int(2) lengthSizeMinusOne; * bit(3) reserved = ‘111’b; * unsigned int(5) numOfSequenceParameterSets; * for (i=0; i< numOfSequenceParameterSets; i++) { * unsigned int(16) sequenceParameterSetLength ; * bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit; * } * unsigned int(8) numOfPictureParameterSets; * for (i=0; i< numOfPictureParameterSets; i++) { * unsigned int(16) pictureParameterSetLength; * bit(8*pictureParameterSetLength) pictureParameterSetNALUnit; * } * } * * * */ try { // TODO: Here we assume that numOfSequenceParameterSets = 1, numOfPictureParameterSets = 1 ! // Here we extract the SPS parameter fis.skipBytes(7); spsLength = 0xFF & fis.readByte(); sps = new byte[spsLength]; fis.read(sps, 0, spsLength); // Here we extract the PPS parameter fis.skipBytes(2); ppsLength = 0xFF & fis.readByte(); pps = new byte[ppsLength]; fis.read(pps, 0, ppsLength); } catch (IOException e) { return false; } return true; }
/** * Constructs a DBFHeader, read the data from file <br> * You need to supply an open file handle to read from * * @param ff open file handle for read access */ DBFHeader(RandomAccessFile ff) throws tinySQLException { try { ff.seek(FLAG_INDEX); file_type = Utils.fixByte(ff.readByte()); // get the last update date file_update_year = Utils.fixByte(ff.readByte()); file_update_month = Utils.fixByte(ff.readByte()); file_update_day = Utils.fixByte(ff.readByte()); // a byte array to hold little-endian long data // byte[] b = new byte[4]; // read that baby in... // ff.readFully(b); // convert the byte array into a long (really a double) // 4-7 number of records numRecords = (int) Utils.vax_to_long(b); // a byte array to hold little-endian short data // b = new byte[2]; // get the data position (where it starts in the file) // 8-9 Length of header ff.readFully(b); headerLength = Utils.vax_to_short(b); // find out the length of the data portion // 10-11 Length of Record ff.readFully(b); recordLength = Utils.vax_to_short(b); // calculate the number of fields // numFields = (headerLength - 33) / 32; // skip the next 20 bytes - looks like this is not needed... // ff.skipBytes(20); // 12-31 reserved Utils.log("HEADER=" + this.toString()); } catch (Exception e) { throw new tinySQLException(e.getMessage()); } }
/** * @param skip 跳过多少过字节进行插入数据 * @param str 要插入的字符串 * @param fileName 文件路径 */ public static void writeSkip(long skip, String str, String fileName) throws IOException { RandomAccessFile raf = null; try { raf = new RandomAccessFile(fileName, "rw"); if (skip < 0 || skip > raf.length()) { System.out.println("跳过字节数无效"); return; } byte[] b = str.getBytes(); raf.setLength(raf.length() + b.length); for (long i = raf.length() - 1; i > b.length + skip - 1; i--) { raf.seek(i - b.length); byte temp = raf.readByte(); raf.seek(i); raf.writeByte(temp); } raf.seek(skip); raf.write(b); } catch (Exception e) { throw new IOException(e); } finally { try { raf.close(); } catch (IOException e) { throw e; } } }
protected final String tail(File file) throws FileNotFoundException, IOException { RandomAccessFile fileHandler = new RandomAccessFile(file, "r"); long fileLength = file.length() - 1; StringBuilder sb = new StringBuilder(); for (long filePointer = fileLength; filePointer != -1; filePointer--) { fileHandler.seek(filePointer); int readByte = fileHandler.readByte(); if (readByte == 0xA) { if (filePointer == fileLength) { continue; } else { break; } } else if (readByte == 0xD) { if (filePointer == fileLength - 1) { continue; } else { break; } } sb.append((char) readByte); } String lastLine = sb.reverse().toString(); return lastLine; }
private void loadFromFile() throws IOException, ReflectiveOperationException { RandomAccessFile file = null; try { int fieldId = 0; file = new RandomAccessFile(filePath, "r"); while (file.getFilePointer() < file.length()) { String fieldName = file.readUTF(); int fieldType = file.readUnsignedByte(); BitSet bitSet = BitSet.valueOf(new byte[] {file.readByte()}); boolean indexed = bitSet.get(BIT_INDEX_INDEXED); boolean stored = bitSet.get(BIT_INDEX_STORED); Field f = new Field(); f.setFieldId(fieldId); f.setFieldName(fieldName); f.setIsIndexed(indexed); f.setIsStored(stored); f.setFieldType(fieldTypeStore.findTypeById(fieldType)); fieldIdMapping.add(f); fieldNameMapping.put(f.getFieldName(), f); fieldId++; } } finally { if (file != null) { file.close(); } } }
public void loadMetaData() throws IOException { pageFile.seek(offset); nextPageId = pageFile.readInt(); currentFill = pageFile.readInt(); bloomfilter = pageFile.readInt(); type = pageFile.readByte(); }
private int peek() throws IOException { if (checkPos(this.pointer)) { buf.seek(pointer++); return b2i(buf.readByte()); } else { return -1; } }
/** {@inheritDoc} */ public byte readByte() throws IOException { --fileSize; if (fileSize < 0) { throw new EOFException(); } else { return dataInput.readByte(); } }
/** * 从offset偏移开始解析后面的字节,读出一个地区名 * * @param offset * @return 地区名字符串 * @throws IOException */ private String readArea(long offset) throws IOException { ipFile.seek(offset); byte b = ipFile.readByte(); if (b == 0x01 || b == 0x02) { long areaOffset = readLong3(offset + 1); if (areaOffset == 0) return "未知"; else return readString(areaOffset); } else return readString(offset); }
/** * 从offset偏移开始解析后面的字节,读出一个地区名 * * @param offset 地区记录的起始偏移 * @return 地区名字符串 * @throws IOException */ private String readArea(long offset) throws IOException { ipFile.seek(offset); byte b = ipFile.readByte(); if (b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) { long areaOffset = readLong3(offset + 1); if (areaOffset == 0) return Message.unknown_area; else return readString(areaOffset); } else return readString(offset); }
public boolean add_song_from_file(String a_file_path) { RandomAccessFile m_file = null; try { m_file = new RandomAccessFile(a_file_path, "r"); byte l_file_name[] = new byte[8]; m_file.read(l_file_name); byte l_file_version = m_file.readByte(); byte l_buffer[] = new byte[0x8000 * 4]; int l_bytes_read = m_file.read(l_buffer); int l_blocks_read = l_bytes_read / g_block_size; if (l_blocks_read > get_free_blocks() || !has_free_slot()) { return false; } byte l_free_slot = get_free_slot(); int l_file_name_ptr = g_file_name_start_ptr + l_free_slot * g_file_name_length; m_work_ram[l_file_name_ptr++] = l_file_name[0]; m_work_ram[l_file_name_ptr++] = l_file_name[1]; m_work_ram[l_file_name_ptr++] = l_file_name[2]; m_work_ram[l_file_name_ptr++] = l_file_name[3]; m_work_ram[l_file_name_ptr++] = l_file_name[4]; m_work_ram[l_file_name_ptr++] = l_file_name[5]; m_work_ram[l_file_name_ptr++] = l_file_name[6]; m_work_ram[l_file_name_ptr++] = l_file_name[7]; int l_file_version_ptr = g_file_version_start_ptr + l_free_slot; m_work_ram[l_file_version_ptr] = l_file_version; int l_blocks_to_write = l_blocks_read; int l_buffer_index = 0; int l_next_block_id_ptr = 0; while (l_blocks_to_write-- > 0) { int l_block_id = get_block_id_of_first_free_block(); if (0 != l_next_block_id_ptr) { // add one to compensate for unused FAT block m_work_ram[l_next_block_id_ptr] = (byte) (l_block_id + 1); } m_work_ram[g_block_alloc_table_start_ptr + l_block_id] = l_free_slot; int l_block_ptr = g_block_start_ptr + l_block_id * g_block_size; for (int l_byte = 0; l_byte < g_block_size; l_byte++) { m_work_ram[l_block_ptr++] = l_buffer[l_buffer_index++]; } l_next_block_id_ptr = get_next_block_id_ptr(l_block_id); } m_file.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
public String getKeyFromFile(RandomAccessFile dataFile) throws IOException { byte ch = 0; ArrayList<Byte> v = new ArrayList<Byte>(); ch = dataFile.readByte(); while (ch != 0) { v.add(ch); ch = dataFile.readByte(); } byte[] res = new byte[v.size()]; for (int i = 0; i < v.size(); i++) { res[i] = v.get(i).byteValue(); } String result = new String(res, StandardCharsets.UTF_8); if (!isCorrectPlace(result)) { IOException e = new IOException(); throw e; } return result; }
private static int nullTerminatorIndex(long offset, RandomAccessFile raf) throws IOException { raf.seek(offset); int index = 0; byte b; do { b = raf.readByte(); index++; } while (b != 0); return index; }
/* * gets an (uncompressed) stream representing the chunk data returns null if * the chunk is not found or an error occurs */ public synchronized DataInputStream getChunkDataInputStream(int x, int z) { if (outOfBounds(x, z)) { debugln("READ", x, z, "out of bounds"); return null; } try { int offset = getOffset(x, z); if (offset == 0) { // debugln("READ", x, z, "miss"); return null; } int sectorNumber = offset >> 8; int numSectors = offset & 0xFF; if (sectorNumber + numSectors > sectorFree.size()) { debugln("READ", x, z, "invalid sector"); return null; } file.seek(sectorNumber * SECTOR_BYTES); int length = file.readInt(); if (length > SECTOR_BYTES * numSectors) { debugln("READ", x, z, "invalid length: " + length + " > 4096 * " + numSectors); return null; } byte version = file.readByte(); if (version == VERSION_GZIP) { byte[] data = new byte[length - 1]; file.read(data); DataInputStream ret = new DataInputStream( new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(data)))); // debug("READ", x, z, " = found"); return ret; } else if (version == VERSION_DEFLATE) { byte[] data = new byte[length - 1]; file.read(data); DataInputStream ret = new DataInputStream( new BufferedInputStream(new InflaterInputStream(new ByteArrayInputStream(data)))); // debug("READ", x, z, " = found"); return ret; } debugln("READ", x, z, "unknown version " + version); return null; } catch (IOException e) { debugln("READ", x, z, "exception"); return null; } }
/** Expect NumBytes data. */ public int Expect(String Data, int NumBytes) { byte target = 0; int cnt = 0; try { while ((NumBytes--) != 0) { target = file.readByte(); if (target != Data.charAt(cnt++)) return DDC_FILE_ERROR; } } catch (IOException ioe) { return DDC_FILE_ERROR; } return DDC_SUCCESS; }
public static long checksumRandomAccessFile(Path filename) throws IOException { try (RandomAccessFile file = new RandomAccessFile(filename.toFile(), "r")) { long length = file.length(); CRC32 crc = new CRC32(); for (long p = 0; p < length; p++) { file.seek(p); int c = file.readByte(); crc.update(c); } return crc.getValue(); } }
private void findPrevLine() throws IOException { if (lastChar == -1) { in.seek(lastPosInFile); lastChar = in.readByte(); } currentLineEnd = currentLineStart; // There are no more lines, since we are at the beginning of the file and no lines. if (currentLineEnd == 0) { currentLineEnd = -1; currentLineStart = -1; currentPos = -1; return; } long filePointer = currentLineStart - 1; while (true) { filePointer--; // we are at start of file so this is the first line in the file. if (filePointer < 0) { break; } in.seek(filePointer); int readByte = in.readByte(); // We ignore last LF in file. search back to find the previous LF. if (readByte == 0xA && filePointer != lastPosInFile) { break; } } // we want to start at pointer +1 so we are after the LF we found or at 0 the start of the file. currentLineStart = filePointer + 1; currentPos = currentLineStart; }
public int read(byte[] b) throws IOException { if (buf == null) throw new IOException("Data buffer not initialized."); if (pointer < 0 || pointer >= length) return -1; int length = this.length - (int) pointer; if (length > b.length) length = b.length; for (int i = 0; i < length; i++) { buf.seek(pointer++); b[i] = buf.readByte(); } return length; }
public PSTFile(File fileName) throws FileNotFoundException, PSTException, IOException { // attempt to open the file. in = new RandomAccessFile(fileName, "r"); // get the first 4 bytes, should be !BDN try { byte[] temp = new byte[4]; in.read(temp); String strValue = new String(temp); if (!strValue.equals("!BDN")) { throw new PSTException("Invalid file header: " + strValue + ", expected: !BDN"); } // make sure we are using a supported version of a PST... byte[] fileTypeBytes = new byte[2]; in.seek(10); in.read(fileTypeBytes); // ANSI file types can be 14 or 15: if (fileTypeBytes[0] == PSTFile.PST_TYPE_ANSI_2) { fileTypeBytes[0] = PSTFile.PST_TYPE_ANSI; } if (fileTypeBytes[0] != PSTFile.PST_TYPE_ANSI && fileTypeBytes[0] != PSTFile.PST_TYPE_UNICODE) { throw new PSTException("Unrecognised PST File version: " + fileTypeBytes[0]); } this.pstFileType = fileTypeBytes[0]; // make sure encryption is turned off at this stage... if (this.getPSTFileType() == PST_TYPE_ANSI) { in.seek(461); } else { in.seek(513); } encryptionType = in.readByte(); if (encryptionType == 0x02) { throw new PSTException( "Only unencrypted and compressable PST files are supported at this time"); } // build out name to id map. processNameToIdMap(in); } catch (IOException err) { throw new PSTException("Unable to read PST Sig", err); } }
public String tailNLines(File file, int lines) { try { java.io.RandomAccessFile fileHandler = new java.io.RandomAccessFile(file, "r"); long fileLength = file.length() - 1; StringBuilder sb = new StringBuilder(); int line = 0; for (long filePointer = fileLength; filePointer != -1; filePointer--) { fileHandler.seek(filePointer); int readByte = fileHandler.readByte(); // if (readByte == 0xA) { // if (line == lines) { // if (filePointer == fileLength) { // continue; // } else { // break; // } // } //// } else if (readByte == 0xD) { // } else if (readByte == Constants.LINE_BREAK) { line = line + 1; if (line == lines) { if (filePointer == fileLength - 1) { continue; } else { break; } } } sb.append((char) readByte); } sb.deleteCharAt(sb.length() - 1); final String lastLine = sb.reverse().toString(); return lastLine; } catch (java.io.FileNotFoundException e) { // logger.error(e); return null; } catch (java.io.IOException e) { // logger.error(e); return null; } }
/** * check if the EUID has been assigned for a given SYSID or system record * * @param sysid * @return */ public boolean isAssigned(long sysid) { try { long filePointer = (sysid - 1) * (euidLenght + INDEX_FIELD_SIZE); long lastRecordPointer = raf.length() - (euidLenght + INDEX_FIELD_SIZE); if (filePointer > lastRecordPointer) { return false; } else { seek(filePointer); byte b = raf.readByte(); return Character.isDigit(b); } } catch (Exception ex) { logger.info(ex.getMessage()); } return true; }
public int read() throws IOException { if (currentPos < currentLineEnd) { in.seek(currentPos++); int readByte = in.readByte(); return readByte; } else if (currentPos > lastPosInFile && currentLineStart < currentLineEnd) { // last line in file (first returned) findPrevLine(); if (lastChar != '\n' && lastChar != '\r') { // last line is not terminated return '\n'; } else { return read(); } } else if (currentPos < 0) { return -1; } else { findPrevLine(); return read(); } }
// see DbFile.java for javadocs public Page readPage(PageId pid) { // some code goes here if (pid.pageNumber() >= numPages()) { throw new IllegalArgumentException("page not in file"); } Page returnme = null; byte[] data = HeapPage.createEmptyPageData(); long offset = (long) BufferPool.PAGE_SIZE * pid.pageNumber(); try { raf.seek(offset); for (int i = 0; i < data.length; i++) { data[i] = raf.readByte(); } returnme = new HeapPage((HeapPageId) pid, data); } catch (EOFException eofe) { eofe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } return returnme; }
public static OggPageHeader read(RandomAccessFile raf) throws IOException, CannotReadException { long start = raf.getFilePointer(); logger.info("Trying to read OggPage at:" + start); byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length]; raf.read(b); if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) { throw new CannotReadException( "OggS Header could not be found, not an ogg stream:" + new String(b)); } raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS); int pageSegments = raf.readByte() & 0xFF; // unsigned raf.seek(start); b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments]; raf.read(b); OggPageHeader pageHeader = new OggPageHeader(b); // Now just after PageHeader, ready for Packet Data return pageHeader; }