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          super(RandomAccessMode.READ);
49          this.fs = fs;
50          this.path = path;
51          this.fis = this.fs.open(this.path);
52      }
53  
54      /**
55       * @see org.apache.commons.vfs2.RandomAccessContent#close()
56       */
57      @Override
58      public void close() throws IOException {
59          this.fis.close();
60      }
61  
62      /**
63       * @see org.apache.commons.vfs2.RandomAccessContent#getFilePointer()
64       */
65      @Override
66      public long getFilePointer() throws IOException {
67          return this.fis.getPos();
68      }
69  
70      /**
71       * @see org.apache.commons.vfs2.RandomAccessContent#getInputStream()
72       */
73      @Override
74      public InputStream getInputStream() throws IOException {
75          return this.fis;
76      }
77  
78      /**
79       * @see org.apache.commons.vfs2.RandomAccessContent#length()
80       */
81      @Override
82      public long length() throws IOException {
83          return this.fs.getFileStatus(this.path).getLen();
84      }
85  
86      /**
87       * @see java.io.DataInput#readBoolean()
88       */
89      @Override
90      public boolean readBoolean() throws IOException {
91          return this.fis.readBoolean();
92      }
93  
94      /**
95       * @see java.io.DataInput#readByte()
96       */
97      @Override
98      public byte readByte() throws IOException {
99          return this.fis.readByte();
100     }
101 
102     /**
103      * @see java.io.DataInput#readChar()
104      */
105     @Override
106     public char readChar() throws IOException {
107         return this.fis.readChar();
108     }
109 
110     /**
111      * @see java.io.DataInput#readDouble()
112      */
113     @Override
114     public double readDouble() throws IOException {
115         return this.fis.readDouble();
116     }
117 
118     /**
119      * @see java.io.DataInput#readFloat()
120      */
121     @Override
122     public float readFloat() throws IOException {
123         return this.fis.readFloat();
124     }
125 
126     /**
127      * @see java.io.DataInput#readFully(byte[])
128      */
129     @Override
130     public void readFully(final byte[] b) throws IOException {
131         throw new UnsupportedOperationException();
132     }
133 
134     /**
135      * @see java.io.DataInput#readFully(byte[], int, int)
136      */
137     @Override
138     public void readFully(final byte[] b, final int off, final int len) throws IOException {
139         throw new UnsupportedOperationException();
140     }
141 
142     /**
143      * @see java.io.DataInput#readInt()
144      */
145     @Override
146     public int readInt() throws IOException {
147         return this.fis.readInt();
148     }
149 
150     /**
151      * @see java.io.DataInput#readLine()
152      */
153     @Override
154     @SuppressWarnings("deprecation")
155     public String readLine() throws IOException {
156         return this.fis.readLine();
157     }
158 
159     /**
160      * @see java.io.DataInput#readLong()
161      */
162     @Override
163     public long readLong() throws IOException {
164         return this.fis.readLong();
165     }
166 
167     /**
168      * @see java.io.DataInput#readShort()
169      */
170     @Override
171     public short readShort() throws IOException {
172         return this.fis.readShort();
173     }
174 
175     /**
176      * @see java.io.DataInput#readUnsignedByte()
177      */
178     @Override
179     public int readUnsignedByte() throws IOException {
180         return this.fis.readUnsignedByte();
181     }
182 
183     /**
184      * @see java.io.DataInput#readUnsignedShort()
185      */
186     @Override
187     public int readUnsignedShort() throws IOException {
188         return this.fis.readUnsignedShort();
189     }
190 
191     /**
192      * @see java.io.DataInput#readUTF()
193      */
194     @Override
195     public String readUTF() throws IOException {
196         return this.fis.readUTF();
197     }
198 
199     /**
200      * @see org.apache.commons.vfs2.RandomAccessContent#seek(long)
201      */
202     @Override
203     public void seek(final long pos) throws IOException {
204         this.fis.seek(pos);
205     }
206 
207     /**
208      * @see org.apache.commons.vfs2.RandomAccessContent#setLength(long)
209      */
210     @Override
211     public void setLength(final long newLength) throws IOException {
212         throw new UnsupportedOperationException();
213     }
214 
215     /**
216      * @see java.io.DataInput#skipBytes(int)
217      */
218     @Override
219     public int skipBytes(final int n) throws IOException {
220         throw new UnsupportedOperationException();
221     }
222 
223 }