001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider.hdfs;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.commons.vfs2.provider.AbstractRandomAccessContent;
023import org.apache.commons.vfs2.util.RandomAccessMode;
024import org.apache.hadoop.fs.FSDataInputStream;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027
028/**
029 * Provides random access to content in an HdfsFileObject. Currently this only supports read operations. All write
030 * operations throw an {@link UnsupportedOperationException}.
031 *
032 * @since 2.1
033 */
034public class HdfsRandomAccessContent extends AbstractRandomAccessContent {
035
036    private final FSDataInputStream fis;
037    private final FileSystem fs;
038    private final Path path;
039
040    /**
041     * Create random access content.
042     *
043     * @param path A Hadoop Path
044     * @param fs A Hadoop FileSystem
045     * @throws IOException when the path cannot be processed.
046     */
047    public HdfsRandomAccessContent(final Path path, final FileSystem fs) throws IOException {
048        super(RandomAccessMode.READ);
049        this.fs = fs;
050        this.path = path;
051        fis = this.fs.open(this.path);
052    }
053
054    /**
055     * @see org.apache.commons.vfs2.RandomAccessContent#close()
056     */
057    @Override
058    public void close() throws IOException {
059        fis.close();
060    }
061
062    /**
063     * @see org.apache.commons.vfs2.RandomAccessContent#getFilePointer()
064     */
065    @Override
066    public long getFilePointer() throws IOException {
067        return fis.getPos();
068    }
069
070    /**
071     * @see org.apache.commons.vfs2.RandomAccessContent#getInputStream()
072     */
073    @Override
074    public InputStream getInputStream() throws IOException {
075        return fis;
076    }
077
078    /**
079     * @see org.apache.commons.vfs2.RandomAccessContent#length()
080     */
081    @Override
082    public long length() throws IOException {
083        return fs.getFileStatus(path).getLen();
084    }
085
086    /**
087     * @see java.io.DataInput#readBoolean()
088     */
089    @Override
090    public boolean readBoolean() throws IOException {
091        return fis.readBoolean();
092    }
093
094    /**
095     * @see java.io.DataInput#readByte()
096     */
097    @Override
098    public byte readByte() throws IOException {
099        return fis.readByte();
100    }
101
102    /**
103     * @see java.io.DataInput#readChar()
104     */
105    @Override
106    public char readChar() throws IOException {
107        return fis.readChar();
108    }
109
110    /**
111     * @see java.io.DataInput#readDouble()
112     */
113    @Override
114    public double readDouble() throws IOException {
115        return fis.readDouble();
116    }
117
118    /**
119     * @see java.io.DataInput#readFloat()
120     */
121    @Override
122    public float readFloat() throws IOException {
123        return 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 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 fis.readLine();
157    }
158
159    /**
160     * @see java.io.DataInput#readLong()
161     */
162    @Override
163    public long readLong() throws IOException {
164        return fis.readLong();
165    }
166
167    /**
168     * @see java.io.DataInput#readShort()
169     */
170    @Override
171    public short readShort() throws IOException {
172        return fis.readShort();
173    }
174
175    /**
176     * @see java.io.DataInput#readUnsignedByte()
177     */
178    @Override
179    public int readUnsignedByte() throws IOException {
180        return fis.readUnsignedByte();
181    }
182
183    /**
184     * @see java.io.DataInput#readUnsignedShort()
185     */
186    @Override
187    public int readUnsignedShort() throws IOException {
188        return fis.readUnsignedShort();
189    }
190
191    /**
192     * @see java.io.DataInput#readUTF()
193     */
194    @Override
195    public String readUTF() throws IOException {
196        return 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        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}