1
1
/*
2
2
* Copyright (c) 2010. Stephen Connolly.
3
- *
3
+ *
4
4
* This library is free software; you can redistribute it and/or
5
5
* modify it under the terms of the GNU Lesser General Public
6
6
* License as published by the Free Software Foundation; either
7
7
* version 2.1 of the License, or (at your option) any later version.
8
- *
8
+ *
9
9
* This library is distributed in the hope that it will be useful,
10
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
12
* Lesser General Public License for more details.
13
- *
13
+ *
14
14
* You should have received a copy of the GNU Lesser General Public
15
15
* License along with this library; if not, write to the Free Software
16
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
17
*/
18
18
19
19
package com .github .stephenc .javaisotools .loopfs .iso9660 ;
20
20
21
- import java .io .File ;
22
- import java .io .FileInputStream ;
23
- import java .io .InputStream ;
24
- import java .util .Properties ;
21
+ import static org .hamcrest .CoreMatchers .is ;
22
+ import static org .junit .Assert .assertThat ;
23
+ import static org .junit .Assume .assumeTrue ;
25
24
25
+ import com .github .stephenc .javaisotools .loopfs .spi .SeekableInputFile ;
26
26
import com .github .stephenc .javaisotools .loopfs .spi .SeekableInputFileHadoop ;
27
+ import com .google .common .collect .Iterables ;
27
28
import org .apache .hadoop .conf .Configuration ;
28
29
import org .apache .hadoop .fs .Path ;
29
30
import org .apache .hadoop .hdfs .MiniDFSCluster ;
30
- import org .junit .*;
31
-
32
31
import org .codehaus .plexus .util .IOUtil ;
32
+ import org .junit .BeforeClass ;
33
+ import org .junit .Test ;
33
34
34
- import static org .hamcrest .CoreMatchers .*;
35
- import static org .junit .Assert .*;
36
- import static org .junit .Assume .assumeTrue ;
35
+ import java .io .File ;
36
+ import java .io .FileInputStream ;
37
+ import java .io .IOException ;
38
+ import java .io .InputStream ;
39
+ import java .util .Properties ;
37
40
38
41
/**
39
42
* Tests the Iso9660 implementation.
@@ -65,13 +68,25 @@ public void smokes() throws Exception {
65
68
this .runCheck (image );
66
69
}
67
70
71
+ @ Test
72
+ public void shouldReadAllBytesWhenSeekableInputPartiallyReads () throws IOException {
73
+ // Create seekeable input which does not read up to specified length
74
+ SeekableInputFile input = new PartiallyReadSeekableInput ();
75
+ Iso9660FileSystem fs = new Iso9660FileSystem (input , true );
76
+ Iso9660FileEntry entry = Iterables .getLast (fs );
77
+
78
+ byte [] bytes = fs .getBytes (entry );
79
+
80
+ assertThat ("All bytes should have been read" , new String (bytes ), is ("Goodbye" ));
81
+ }
82
+
68
83
@ Test
69
84
public void hdfsSmokes () throws Exception {
70
85
assumeTrue (isNotWindows ());
71
86
//Creating a Mini DFS Cluster as the default File System does not return a Seekable Stream
72
87
MiniDFSCluster .Builder builder = new MiniDFSCluster .Builder (new Configuration ());
73
88
MiniDFSCluster hdfsCluster = builder .build ();
74
- String hdfsTestFile = "hdfs://127.0.0.1:" + hdfsCluster .getNameNodePort () + "/test/" + filePath ;
89
+ String hdfsTestFile = "hdfs://127.0.0.1:" + hdfsCluster .getNameNodePort () + "/test/" + filePath ;
75
90
hdfsCluster .getFileSystem ()
76
91
.copyFromLocalFile (new Path (filePath ), new Path (hdfsTestFile ));
77
92
InputStream is = hdfsCluster .getFileSystem ().open (new Path (hdfsTestFile ));
@@ -97,4 +112,22 @@ private boolean isNotWindows() {
97
112
String os = System .getProperty ("os.name" );
98
113
return !os .startsWith ("Windows" );
99
114
}
115
+
116
+ private static class PartiallyReadSeekableInput extends SeekableInputFile {
117
+
118
+ private byte [] bytes ;
119
+
120
+ public PartiallyReadSeekableInput () throws IOException {
121
+ super (new File (filePath ));
122
+ }
123
+
124
+ @ Override
125
+ public int read (byte [] b , int off , int len ) throws IOException {
126
+ // Deliberately miss last byte on first pass
127
+ boolean firstPass = b != bytes ;
128
+ int length = firstPass ? len - 1 : len ;
129
+ bytes = b ;
130
+ return super .read (b , off , length );
131
+ }
132
+ }
100
133
}
0 commit comments