static public boolean isValidFile(ucar.unidata.io.RandomAccessFile raf) throws IOException { // this is the first time we try to read the file - if there's a problem we get a IOException raf.seek(0); byte[] b = new byte[4]; raf.read(b); for (int i = 0; i < 3; i++) if (b[i] != MAGIC[i]) return false; return ((b[3] == 1) || (b[3] == 2)); }
/** * Constructor * * @param raf the RandomAccessFile * @param startPos points to start of data in data section, in bytes * @throws IOException on read error */ public BitReader(RandomAccessFile raf, long startPos) throws IOException { this.raf = raf; this.startPos = startPos; raf.seek(startPos); }
private long readVariableSizeN(int nbytes) throws IOException { int[] ch = new int[nbytes]; for (int i = 0; i < nbytes; i++) ch[i] = raf.read(); long result = ch[nbytes - 1]; for (int i = nbytes - 2; i >= 0; i--) { result = result << 8; result += ch[i]; } return result; }
public long getPos() throws IOException { if (raf != null) { return raf.getFilePointer(); } else { return dataPos; } }
/** * Returns the short resulting from swapping 2 bytes of a specified * short. * * @param s input value for which byte reversal is desired * @return the value represented by the bytes of <code>s</code> * reversed */ static public short swapShort(short s) { return (swapShort(shortToBytes(s), 0)); }
/** * Returns the int resulting from reversing 4 bytes of a specified * int. * * @param v input value for which byte reversal is desired * @return the value represented by the bytes of <code>v</code> * reversed */ static public int swapInt(int v) { return (swapInt(intToBytes(v), 0)); }
/** * Constructor, for subclasses * * @param bufferSize size of read buffer */ protected RandomAccessFile(int bufferSize) { file = null; readonly = true; init(bufferSize); }
/** * Constructor * @param match search for this byte pattern */ public KMPMatch(byte[] match) { this.match = match; failure = computeFailure(match); }
@Override public long skip(long num) throws IOException { byte[] tmp = new byte[(int) num]; int got = read(tmp, 0, (int) num); if (got > 0) return (long) got; else return 0L; }
/** * Close the file. * @throws IOException */ public void close() throws IOException { if (raf != null) { raf.close(); } }
/** * Writes an <code>int</code> to the file as four bytes, high byte first. * * @param v an <code>int</code> to be written. * @throws IOException if an I/O error occurs. */ public final void writeInt(int v) throws IOException { write((v >>> 24) & 0xFF); write((v >>> 16) & 0xFF); write((v >>> 8) & 0xFF); write((v) & 0xFF); }