Bringing tree up to date.
[galago.git] / java / pig-galago / src / com / yahoo / pig / impl / io / InputStreamPosition.java
blobc7b89ef9cff7c3aa00b731afafe94d3da2deafe5
1 /*
2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
4 */
5 package com.yahoo.pig.impl.io;
7 import java.io.FilterInputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
11 /**
12 * A simple filter stream that keeps track of the position. It is not at
13 * all specific to Pig, but the need for it arises when implementing LoadFuncs.
15 * @author breed
18 public class InputStreamPosition extends FilterInputStream {
19 long pos;
20 long markedPos;
21 /**
22 * Creates a InputStream filter for the given InputStream with
23 * the given initial position.
25 public InputStreamPosition(InputStream in, long pos) {
26 super(in);
27 this.pos = pos;
29 public synchronized void mark(int readlimit) {
30 super.mark(readlimit);
31 markedPos = pos;
33 public int read() throws IOException {
34 int c = super.read();
35 pos++;
36 return c;
38 public int read(byte[] b, int off, int len) throws IOException {
39 int rc = super.read(b, off, len);
40 if (rc > 0) {
41 pos += rc;
43 return rc;
45 public synchronized void reset() throws IOException {
46 super.reset();
47 pos = markedPos;
49 public long skip(long n) throws IOException {
50 long rc = super.skip(n);
51 pos += rc;
52 return rc;
55 /**
56 * Returns the current position in the tracked InputStream.
58 public long getPosition() {
59 return pos;