2
2
3
3
import java .io .IOException ;
4
4
5
+ import org .apache .hadoop .fs .FSExceptionMessages ;
5
6
import org .slf4j .Logger ;
6
7
import org .slf4j .LoggerFactory ;
7
8
14
15
public class S3ASeekableStream extends FSInputStream {
15
16
16
17
private S3SeekableInputStream inputStream ;
18
+ private long lastReadCurrentPos = 0 ;
17
19
private final String key ;
18
20
19
21
public static final Logger LOG = LoggerFactory .getLogger (S3ASeekableStream .class );
20
22
21
-
22
- public S3ASeekableStream (String bucket , String key , S3SeekableInputStreamFactory s3SeekableInputStreamFactory )
23
- throws IOException {
23
+ public S3ASeekableStream (String bucket , String key , S3SeekableInputStreamFactory s3SeekableInputStreamFactory ) {
24
24
this .inputStream = s3SeekableInputStreamFactory .createStream (S3URI .of (bucket , key ));
25
25
this .key = key ;
26
26
}
27
27
28
28
@ Override
29
29
public int read () throws IOException {
30
+ throwIfClosed ();
30
31
return inputStream .read ();
31
32
}
32
33
33
34
@ Override
34
35
public void seek (long pos ) throws IOException {
36
+ throwIfClosed ();
35
37
inputStream .seek (pos );
36
38
}
37
39
38
- @ Override
39
- public long getPos () throws IOException {
40
- return inputStream .getPos ();
41
- }
42
40
43
41
@ Override
44
- public void close () throws IOException {
45
- if (inputStream != null ) {
46
- inputStream .close ();
47
- inputStream = null ;
48
- super .close ();
42
+ public synchronized long getPos () {
43
+ if (!isClosed ()) {
44
+ lastReadCurrentPos = inputStream .getPos ();
49
45
}
46
+ return lastReadCurrentPos ;
50
47
}
51
48
52
49
53
50
public void readTail (byte [] buf , int off , int n ) throws IOException {
51
+ throwIfClosed ();
54
52
inputStream .readTail (buf , off , n );
55
53
}
56
54
57
55
@ Override
58
56
public int read (byte [] buf , int off , int len ) throws IOException {
57
+ throwIfClosed ();
59
58
return inputStream .read (buf , off , len );
60
59
}
61
60
@@ -65,4 +64,22 @@ public boolean seekToNewSource(long l) throws IOException {
65
64
return false ;
66
65
}
67
66
67
+ @ Override
68
+ public void close () throws IOException {
69
+ if (inputStream != null ) {
70
+ inputStream .close ();
71
+ inputStream = null ;
72
+ super .close ();
73
+ }
74
+ }
75
+
76
+ protected void throwIfClosed () throws IOException {
77
+ if (isClosed ()) {
78
+ throw new IOException (FSExceptionMessages .STREAM_IS_CLOSED );
79
+ }
80
+ }
81
+
82
+ protected boolean isClosed () {
83
+ return inputStream == null ;
84
+ }
68
85
}
0 commit comments