View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.hdfs;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.apache.commons.vfs2.provider.AbstractRandomAccessContent;
23  import org.apache.commons.vfs2.util.RandomAccessMode;
24  import org.apache.hadoop.fs.FSDataInputStream;
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  
28  /**
29   * Provides random access to content in an HdfsFileObject. Currently this only supports read operations. All write
30   * operations throw an {@link UnsupportedOperationException}.
31   *
32   * @since 2.1
33   */
34  public class HdfsRandomAccessContent extends AbstractRandomAccessContent
35  {
36      private final FSDataInputStream fis;
37      private final FileSystem fs;
38      private final Path path;
39  
40      /**
41       * Create random access content.
42       *
43       * @param path A Hadoop Path
44       * @param fs A Hadoop FileSystem
45       * @throws IOException when the path cannot be processed.
46       */
47      public HdfsRandomAccessContent(final Path path, final FileSystem fs) throws IOException
48      {
49          super(RandomAccessMode.READ);
50          this.fs = fs;
51          this.path = path;
52          this.fis = this.fs.open(this.path);
53      }
54  
55      /**
56       * @see org.apache.commons.vfs2.RandomAccessContent#close()
57       */
58      @Override
59      public void close() throws IOException
60      {
61          this.fis.close();
62      }
63  
64      /**
65       * @see org.apache.commons.vfs2.RandomAccessContent#getFilePointer()
66       */
67      @Override
68      public long getFilePointer() throws IOException
69      {
70          return this.fis.getPos();
71      }
72  
73      /**
74       * @see org.apache.commons.vfs2.RandomAccessContent#getInputStream()
75       */
76      @Override
77      public InputStream getInputStream() throws IOException
78      {
79          return this.fis;
80      }
81  
82      /**
83       * @see org.apache.commons.vfs2.RandomAccessContent#length()
84       */
85      @Override
86      public long length() throws IOException
87      {
88          return this.fs.getFileStatus(this.path).getLen();
89      }
90  
91      /**
92       * @see java.io.DataInput#readBoolean()
93       */
94      @Override
95      public boolean readBoolean() throws IOException
96      {
97          return this.fis.readBoolean();
98      }
99  
100     /**
101      * @see java.io.DataInput#readByte()
102      */
103     @Override
104     public byte readByte() throws IOException
105     {
106         return this.fis.readByte();
107     }
108 
109     /**
110      * @see java.io.DataInput#readChar()
111      */
112     @Override
113     public char readChar() throws IOException
114     {
115         return this.fis.readChar();
116     }
117 
118     /**
119      * @see java.io.DataInput#readDouble()
120      */
121     @Override
122     public double readDouble() throws IOException
123     {
124         return this.fis.readDouble();
125     }
126 
127     /**
128      * @see java.io.DataInput#readFloat()
129      */
130     @Override
131     public float readFloat() throws IOException
132     {
133         return this.fis.readFloat();
134     }
135 
136     /**
137      * @see java.io.DataInput#readFully(byte[])
138      */
139     @Override
140     public void readFully(final byte[] b) throws IOException
141     {
142         throw new UnsupportedOperationException();
143     }
144 
145     /**
146      * @see java.io.DataInput#readFully(byte[], int, int)
147      */
148     @Override
149     public void readFully(final byte[] b, final int off, final int len) throws IOException
150     {
151         throw new UnsupportedOperationException();
152     }
153 
154     /**
155      * @see java.io.DataInput#readInt()
156      */
157     @Override
158     public int readInt() throws IOException
159     {
160         return this.fis.readInt();
161     }
162 
163     /**
164      * @see java.io.DataInput#readLine()
165      */
166     @Override
167     @SuppressWarnings("deprecation")
168     public String readLine() throws IOException
169     {
170         return this.fis.readLine();
171     }
172 
173     /**
174      * @see java.io.DataInput#readLong()
175      */
176     @Override
177     public long readLong() throws IOException
178     {
179         return this.fis.readLong();
180     }
181 
182     /**
183      * @see java.io.DataInput#readShort()
184      */
185     @Override
186     public short readShort() throws IOException
187     {
188         return this.fis.readShort();
189     }
190 
191     /**
192      * @see java.io.DataInput#readUnsignedByte()
193      */
194     @Override
195     public int readUnsignedByte() throws IOException
196     {
197         return this.fis.readUnsignedByte();
198     }
199 
200     /**
201      * @see java.io.DataInput#readUnsignedShort()
202      */
203     @Override
204     public int readUnsignedShort() throws IOException
205     {
206         return this.fis.readUnsignedShort();
207     }
208 
209     /**
210      * @see java.io.DataInput#readUTF()
211      */
212     @Override
213     public String readUTF() throws IOException
214     {
215         return this.fis.readUTF();
216     }
217 
218     /**
219      * @see org.apache.commons.vfs2.RandomAccessContent#seek(long)
220      */
221     @Override
222     public void seek(final long pos) throws IOException
223     {
224         this.fis.seek(pos);
225     }
226 
227     /**
228      * @see org.apache.commons.vfs2.RandomAccessContent#setLength(long)
229      */
230     @Override
231     public void setLength(final long newLength) throws IOException
232     {
233         throw new UnsupportedOperationException();
234     }
235 
236     /**
237      * @see java.io.DataInput#skipBytes(int)
238      */
239     @Override
240     public int skipBytes(final int n) throws IOException
241     {
242         throw new UnsupportedOperationException();
243     }
244 
245 }