forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatableFileInputStream.java
More file actions
158 lines (140 loc) · 4.47 KB
/
RepeatableFileInputStream.java
File metadata and controls
158 lines (140 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
* for applicable license terms and NOTICE.txt for applicable notices.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.s3.internal;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A repeatable input stream for files. This input stream can be repeated an
* unlimited number of times, without any limitation on when a repeat can occur.
*/
public class RepeatableFileInputStream extends InputStream {
private static final Log log = LogFactory.getLog(RepeatableFileInputStream.class);
private final File file;
private FileInputStream fis = null;
private long bytesReadPastMarkPoint = 0;
private long markPoint = 0;
/**
* Creates a repeatable input stream based on a file.
*
* @param file
* The file from which this input stream reads data.
*
* @throws FileNotFoundException
* If the specified file doesn't exist, or can't be opened.
*/
public RepeatableFileInputStream(File file) throws FileNotFoundException {
if (file == null) {
throw new IllegalArgumentException("File cannot be null");
}
this.fis = new FileInputStream(file);
this.file = file;
}
/**
* Returns the File this stream is reading data from.
*
* @return the File this stream is reading data from.
*/
public File getFile() {
return file;
}
/**
* Resets the input stream to the last mark point, or the beginning of the
* stream if there is no mark point, by creating a new FileInputStream based
* on the underlying file.
*
* @throws IOException
* when the FileInputStream cannot be re-created.
*/
public void reset() throws IOException {
this.fis.close();
this.fis = new FileInputStream(file);
long skipped = 0;
long toSkip = markPoint;
while (toSkip > 0) {
skipped = this.fis.skip(toSkip);
toSkip -= skipped;
}
if (log.isDebugEnabled()) {
log.debug("Reset to mark point " + markPoint
+ " after returning " + bytesReadPastMarkPoint + " bytes");
}
this.bytesReadPastMarkPoint = 0;
}
/**
* @see java.io.InputStream#markSupported()
*/
public boolean markSupported() {
return true;
}
/**
* @see java.io.InputStream#mark(int)
*/
public void mark(int readlimit) {
this.markPoint += bytesReadPastMarkPoint;
this.bytesReadPastMarkPoint = 0;
if (log.isDebugEnabled()) {
log.debug("Input stream marked at " + this.markPoint + " bytes");
}
}
/**
* @see java.io.InputStream#available()
*/
public int available() throws IOException {
return fis.available();
}
/**
* @see java.io.InputStream#close()
*/
public void close() throws IOException {
fis.close();
}
/**
* @see java.io.InputStream#read()
*/
public int read() throws IOException {
int byteRead = fis.read();
if (byteRead != -1) {
bytesReadPastMarkPoint++;
return byteRead;
} else {
return -1;
}
}
@Override
public long skip(long n) throws IOException {
long skipped = fis.skip(n);
bytesReadPastMarkPoint += skipped;
return skipped;
}
/**
* @see java.io.InputStream#read(byte[], int, int)
*/
public int read(byte[] arg0, int arg1, int arg2) throws IOException {
int count = fis.read(arg0, arg1, arg2);
bytesReadPastMarkPoint += count;
return count;
}
public InputStream getWrappedInputStream() {
return this.fis;
}
}