| 1 | /******************************************************************** |
| 2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Author: Zhanwei Wang |
| 6 | ********************************************************************/ |
| 7 | /******************************************************************** |
| 8 | * 2014 - |
| 9 | * open source under Apache License Version 2.0 |
| 10 | ********************************************************************/ |
| 11 | /** |
| 12 | * Licensed to the Apache Software Foundation (ASF) under one |
| 13 | * or more contributor license agreements. See the NOTICE file |
| 14 | * distributed with this work for additional information |
| 15 | * regarding copyright ownership. The ASF licenses this file |
| 16 | * to you under the Apache License, Version 2.0 (the |
| 17 | * "License"); you may not use this file except in compliance |
| 18 | * with the License. You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | */ |
| 28 | #include "FileSystemImpl.h" |
| 29 | #include "FileSystemInter.h" |
| 30 | #include "InputStream.h" |
| 31 | #include "InputStreamImpl.h" |
| 32 | #include "InputStreamInter.h" |
| 33 | |
| 34 | using namespace Hdfs::Internal; |
| 35 | |
| 36 | namespace Hdfs { |
| 37 | |
| 38 | InputStream::InputStream() { |
| 39 | impl = new Internal::InputStreamImpl; |
| 40 | } |
| 41 | |
| 42 | InputStream::~InputStream() { |
| 43 | delete impl; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Open a file to read |
| 48 | * @param fs hdfs file system. |
| 49 | * @param path the file to be read. |
| 50 | * @param verifyChecksum verify the checksum. |
| 51 | */ |
| 52 | void InputStream::open(FileSystem & fs, const char * path, |
| 53 | bool verifyChecksum) { |
| 54 | if (!fs.impl) { |
| 55 | THROW(HdfsIOException, "FileSystem: not connected." ); |
| 56 | } |
| 57 | |
| 58 | impl->open(fs.impl->filesystem, path, verifyChecksum); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * To read data from hdfs. |
| 63 | * @param buf the buffer used to filled. |
| 64 | * @param size buffer size. |
| 65 | * @return return the number of bytes filled in the buffer, it may less than size. |
| 66 | */ |
| 67 | int32_t InputStream::read(char * buf, int32_t size) { |
| 68 | return impl->read(buf, size); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * To read data from hdfs, block until get the given size of bytes. |
| 73 | * @param buf the buffer used to filled. |
| 74 | * @param size the number of bytes to be read. |
| 75 | */ |
| 76 | void InputStream::readFully(char * buf, int64_t size) { |
| 77 | impl->readFully(buf, size); |
| 78 | } |
| 79 | |
| 80 | int64_t InputStream::available() { |
| 81 | return impl->available(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * To move the file point to the given position. |
| 86 | * @param pos the given position. |
| 87 | */ |
| 88 | void InputStream::seek(int64_t pos) { |
| 89 | impl->seek(pos); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * To get the current file point position. |
| 94 | * @return the position of current file point. |
| 95 | */ |
| 96 | int64_t InputStream::tell() { |
| 97 | return impl->tell(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Close the sthream. |
| 102 | */ |
| 103 | void InputStream::close() { |
| 104 | impl->close(); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |