1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "filestream.hh" |
---|
20 | |
---|
21 | using namespace hadoop; |
---|
22 | |
---|
23 | hadoop::FileInStream::FileInStream() |
---|
24 | { |
---|
25 | mFile = NULL; |
---|
26 | } |
---|
27 | |
---|
28 | bool hadoop::FileInStream::open(const std::string& name) |
---|
29 | { |
---|
30 | mFile = fopen(name.c_str(), "rb"); |
---|
31 | return (mFile != NULL); |
---|
32 | } |
---|
33 | |
---|
34 | ssize_t hadoop::FileInStream::read(void *buf, size_t len) |
---|
35 | { |
---|
36 | return fread(buf, 1, len, mFile); |
---|
37 | } |
---|
38 | |
---|
39 | bool hadoop::FileInStream::skip(size_t nbytes) |
---|
40 | { |
---|
41 | return (0==fseek(mFile, nbytes, SEEK_CUR)); |
---|
42 | } |
---|
43 | |
---|
44 | bool hadoop::FileInStream::close() |
---|
45 | { |
---|
46 | int ret = fclose(mFile); |
---|
47 | mFile = NULL; |
---|
48 | return (ret==0); |
---|
49 | } |
---|
50 | |
---|
51 | hadoop::FileInStream::~FileInStream() |
---|
52 | { |
---|
53 | if (mFile != NULL) { |
---|
54 | close(); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | hadoop::FileOutStream::FileOutStream() |
---|
59 | { |
---|
60 | mFile = NULL; |
---|
61 | } |
---|
62 | |
---|
63 | bool hadoop::FileOutStream::open(const std::string& name, bool overwrite) |
---|
64 | { |
---|
65 | if (!overwrite) { |
---|
66 | mFile = fopen(name.c_str(), "rb"); |
---|
67 | if (mFile != NULL) { |
---|
68 | fclose(mFile); |
---|
69 | return false; |
---|
70 | } |
---|
71 | } |
---|
72 | mFile = fopen(name.c_str(), "wb"); |
---|
73 | return (mFile != NULL); |
---|
74 | } |
---|
75 | |
---|
76 | ssize_t hadoop::FileOutStream::write(const void* buf, size_t len) |
---|
77 | { |
---|
78 | return fwrite(buf, 1, len, mFile); |
---|
79 | } |
---|
80 | |
---|
81 | bool hadoop::FileOutStream::advance(size_t nbytes) |
---|
82 | { |
---|
83 | return (0==fseek(mFile, nbytes, SEEK_CUR)); |
---|
84 | } |
---|
85 | |
---|
86 | bool hadoop::FileOutStream::close() |
---|
87 | { |
---|
88 | int ret = fclose(mFile); |
---|
89 | mFile = NULL; |
---|
90 | return (ret == 0); |
---|
91 | } |
---|
92 | |
---|
93 | hadoop::FileOutStream::~FileOutStream() |
---|
94 | { |
---|
95 | if (mFile != NULL) { |
---|
96 | close(); |
---|
97 | } |
---|
98 | } |
---|