/** Open a reader for a file. */ public static <D> FileReader<D> openReader(SeekableInput in, DatumReader<D> reader) throws IOException { if (in.length() < MAGIC.length) throw new IOException("Not an Avro data file"); // read magic header byte[] magic = new byte[MAGIC.length]; in.seek(0); for (int c = 0; c < magic.length; c = in.read(magic, c, magic.length-c)) {} in.seek(0); if (Arrays.equals(MAGIC, magic)) // current format return new DataFileReader<D>(in, reader); if (Arrays.equals(DataFileReader12.MAGIC, magic)) // 1.2 format return new DataFileReader12<D>(in, reader); throw new IOException("Not an Avro data file"); }
@Override public void close() throws IOException { in.close(); super.close(); }
@Override public long tell() throws IOException { return in.tell(); }
@Override public int read(byte[] b, int off, int len) throws IOException { return in.read(b, off, len); }
@Override public long length() throws IOException { return in.length(); }
@Override public void seek(long p) throws IOException { if (p < 0) throw new IOException("Illegal seek: " + p); in.seek(p); }
@Override public long tell() throws IOException { return in.tell(); }
@Override public int read(byte[] b) throws IOException { return in.read(b, 0, b.length); }
@Override public long length() throws IOException { return in.length(); }
@Override public void seek(long p) throws IOException { if (p < 0) throw new IOException("Illegal seek: " + p); in.seek(p); }
/** Open a reader for a file. */ public static <D> FileReader<D> openReader(SeekableInput in, DatumReader<D> reader) throws IOException { if (in.length() < MAGIC.length) throw new InvalidAvroMagicException("Not an Avro data file"); // read magic header byte[] magic = new byte[MAGIC.length]; in.seek(0); for (int c = 0; c < magic.length; c = in.read(magic, c, magic.length-c)) {} in.seek(0); if (Arrays.equals(MAGIC, magic)) // current format return new DataFileReader<>(in, reader); if (Arrays.equals(DataFileReader12.MAGIC, magic)) // 1.2 format return new DataFileReader12<>(in, reader); throw new InvalidAvroMagicException("Not an Avro data file"); }
/** * Construct a reader for a file at the current position of the input, * without reading the header. * @param sync True to read forward to the next sync point after opening, * false to assume that the input is already at a valid sync * point. */ public static <D> DataFileReader<D> openReader(SeekableInput in, DatumReader<D> reader, Header header, boolean sync) throws IOException { DataFileReader<D> dreader = new DataFileReader<>(in, reader, header); // seek/sync to an (assumed) valid position if (sync) dreader.sync(in.tell()); else dreader.seek(in.tell()); return dreader; }
@Override public int read(byte[] b) throws IOException { return in.read(b, 0, b.length); }
expect(inputSplit.getPath()).andReturn(new Path("/path/to/an/avro/file")).anyTimes(); expect(inputSplit.getStart()).andReturn(0L).anyTimes(); expect(inputSplit.getLength()).andReturn(avroFileInput.length()).anyTimes();
@Override public void seek(long p) throws IOException { if (p < 0) throw new IOException("Illegal seek: " + p); in.seek(p); }