/** * 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()); } }
@Override public ByteBuffer readPage(File file, long position, ByteBuffer pageBuffer) throws IOException, InterruptedException { long start = System.currentTimeMillis(); RandomAccessFile randomAccessFile = randomAccessFile(file); try { randomAccessFile.seek(position); randomAccessFile.readFully(pageBuffer.array(), pageBuffer.arrayOffset(), pageSizeBytes); if (Thread.interrupted()) { throw new InterruptedException(); } long stop = System.currentTimeMillis(); if (LOG.isLoggable(Level.FINE)) { LOG.log( Level.FINE, "Read page at {0} of {1}: {2} msec", new Object[] {position, file, stop - start}); } } catch (EOFException e) { LOG.log( Level.SEVERE, "Caught EOFException while reading {0}, position {1}", new Object[] {file, position}); LOG.log(Level.SEVERE, "stack", e); throw e; } finally { randomAccessFile.close(); } return pageBuffer; }
private void loadRawData() throws IOException { if (rawData == null) { pageHandler.trimPageSet(id); rawData = new byte[PAYLOAD]; pageFile.seek(offset + HEADER); pageFile.readFully(rawData); } }
String read(int i) { if (i >= index || i < 0) return null; byte[] b = new byte[BLOCKSIZE]; try { seek(i * BLOCKSIZE); super.readFully(b); } catch (IOException ie) { } return new String(b).trim(); }
public static byte[] loadFileIntoByteArray(String path) throws IOException { RandomAccessFile f = new RandomAccessFile(path, "r"); try { byte[] data = new byte[(int) f.length()]; f.readFully(data); return data; } finally { f.close(); } }
private static void replacePackageForJavaFile(File path, String oldPackage, String newPackage) { if (path != null && path.exists()) { // 处理文件夹 if (path.isDirectory()) { if ("target".equals(path.getName())) { return; } String[] children = path.list(); for (int i = 0; i < children.length; i++) { File child = new File(path.getPath() + System.getProperty("file.separator") + children[i]); // 递归处理 replacePackageForJavaFile(child, oldPackage, newPackage); } } else { // 处理相关文件 if (path.getName().toLowerCase().endsWith(".java") || path.getName().toLowerCase().endsWith(".jsp") || path.getName().toLowerCase().endsWith(".ftl") || path.getName().toLowerCase().endsWith(".properties") || path.getName().toLowerCase().endsWith(".xml")) { System.out.println(path.getAbsolutePath()); fileCount++; try { byte[] content; try (RandomAccessFile f = new RandomAccessFile(path, "rw")) { content = new byte[(int) f.length()]; f.readFully(content); } String text = new String(content, "utf-8"); // 替换package text = text.replaceAll("package " + oldPackage, "package " + newPackage); // 替换import text = text.replaceAll("import " + oldPackage, "import " + newPackage); // 替换/分割的路径 String _old = oldPackage.split("\\.")[0] + "/" + oldPackage.split("\\.")[1]; String _new = newPackage.split("\\.")[0] + "/" + newPackage.split("\\.")[1]; text = text.replaceAll(_old, _new); // 除了本文件,其他文件要全文替换,这个替换要在最后 if (!path.getName().endsWith("ChangePackageName.java")) { text = text.replaceAll(oldPackage, newPackage); } try (Writer writer = new OutputStreamWriter(new FileOutputStream(path), "utf-8")) { writer.write(text); } } catch (Exception ex) { failCount++; } } } } }
/** * Reads a block from disk. * * @param b block to fetch */ private void readBlock(final int b) { if (!bm.cursor(b)) return; final Buffer bf = bm.current(); try { if (bf.dirty) writeBlock(bf); bf.pos = b; if (b >= blocks) { blocks = b + 1; } else { file.seek(bf.pos * IO.BLOCKSIZE); file.readFully(bf.data); } } catch (final IOException ex) { Util.stack(ex); } }
private static void truncateBlock(File blockFile, File metaFile, long oldlen, long newlen) throws IOException { if (newlen == oldlen) { return; } if (newlen > oldlen) { throw new IOException( "Cannout truncate block to from oldlen (=" + oldlen + ") to newlen (=" + newlen + ")"); } DataChecksum dcs = BlockMetadataHeader.readHeader(metaFile).getChecksum(); int checksumsize = dcs.getChecksumSize(); int bpc = dcs.getBytesPerChecksum(); long n = (newlen - 1) / bpc + 1; long newmetalen = BlockMetadataHeader.getHeaderSize() + n * checksumsize; long lastchunkoffset = (n - 1) * bpc; int lastchunksize = (int) (newlen - lastchunkoffset); byte[] b = new byte[Math.max(lastchunksize, checksumsize)]; RandomAccessFile blockRAF = new RandomAccessFile(blockFile, "rw"); try { // truncate blockFile blockRAF.setLength(newlen); // read last chunk blockRAF.seek(lastchunkoffset); blockRAF.readFully(b, 0, lastchunksize); } finally { blockRAF.close(); } // compute checksum dcs.update(b, 0, lastchunksize); dcs.writeValue(b, 0, false); // update metaFile RandomAccessFile metaRAF = new RandomAccessFile(metaFile, "rw"); try { metaRAF.setLength(newmetalen); metaRAF.seek(newmetalen - checksumsize); metaRAF.write(b, 0, checksumsize); } finally { metaRAF.close(); } }
private static byte[] readFile(String sFile) { byte[] emptybuf = null; try { RandomAccessFile file = new RandomAccessFile(new File(sFile), "r"); long iLen = file.length(); log(sFile + " length is " + iLen); byte[] buf = new byte[(int) iLen]; // int[] buf = new int[(int)iLen]; file.readFully(buf); file.close(); return buf; } catch (IOException es) { log("File not found."); } return emptybuf; }
public static void pager(String fileName, String header, String title, String deleteFile) { HashMap<String, Object> attributes = new HashMap<String, Object>(); byte[] buffer = null; try { RandomAccessFile raf = new RandomAccessFile(fileName, "r"); buffer = new byte[(int) raf.length()]; raf.readFully(buffer); raf.close(); } catch (Exception e) { log.error("Error!", e); } DirectJNI.getInstance() .notifyRActionListeners( new RAction(RActionType.PAGER) .put(RActionConst.FILENAME, new File(fileName).getName()) .put(RActionConst.CONTENT, buffer) .put(RActionConst.HEADER, header) .put(RActionConst.TITLE, title) .put(RActionConst.DELETEFILE, new Boolean(deleteFile))); }
public void readFully(byte[] buffer) throws IOException { raf.readFully(buffer); }