/src/logging-log4cxx/src/main/cpp/fileinputstream.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/helpers/fileinputstream.h> |
20 | | #include <log4cxx/helpers/exception.h> |
21 | | #include <log4cxx/helpers/bytebuffer.h> |
22 | | #include <log4cxx/helpers/pool.h> |
23 | | #include <apr_file_io.h> |
24 | | |
25 | | using namespace LOG4CXX_NS; |
26 | | using namespace LOG4CXX_NS::helpers; |
27 | | |
28 | | struct FileInputStream::FileInputStreamPrivate |
29 | | { |
30 | 0 | FileInputStreamPrivate() : fileptr(nullptr) {} |
31 | | |
32 | | Pool pool; |
33 | | apr_file_t* fileptr; |
34 | | }; |
35 | | |
36 | | IMPLEMENT_LOG4CXX_OBJECT(FileInputStream) |
37 | | |
38 | | FileInputStream::FileInputStream(const LogString& filename) : |
39 | 0 | m_priv(std::make_unique<FileInputStreamPrivate>()) |
40 | 0 | { |
41 | 0 | open(filename); |
42 | 0 | } |
43 | | |
44 | | FileInputStream::FileInputStream(const logchar* filename) : |
45 | 0 | m_priv(std::make_unique<FileInputStreamPrivate>()) |
46 | 0 | { |
47 | 0 | LogString fn(filename); |
48 | 0 | open(fn); |
49 | 0 | } |
50 | | |
51 | | |
52 | | void FileInputStream::open(const LogString& filename) |
53 | 0 | { |
54 | 0 | apr_fileperms_t perm = APR_OS_DEFAULT; |
55 | 0 | apr_int32_t flags = APR_READ; |
56 | 0 | apr_status_t stat = File().setPath(filename).open(&m_priv->fileptr, flags, perm, m_priv->pool); |
57 | |
|
58 | 0 | if (stat != APR_SUCCESS) |
59 | 0 | { |
60 | 0 | throw IOException(filename, stat); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | |
65 | | FileInputStream::FileInputStream(const File& aFile) : |
66 | 0 | m_priv(std::make_unique<FileInputStreamPrivate>()) |
67 | 0 | { |
68 | 0 | apr_fileperms_t perm = APR_OS_DEFAULT; |
69 | 0 | apr_int32_t flags = APR_READ; |
70 | 0 | apr_status_t stat = aFile.open(&m_priv->fileptr, flags, perm, m_priv->pool); |
71 | |
|
72 | 0 | if (stat != APR_SUCCESS) |
73 | 0 | { |
74 | 0 | throw IOException(aFile.getName(), stat); |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | | |
79 | | FileInputStream::~FileInputStream() |
80 | 0 | { |
81 | 0 | if (m_priv->fileptr) |
82 | 0 | { |
83 | 0 | apr_file_close(m_priv->fileptr); |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | | |
88 | | void FileInputStream::close() |
89 | 0 | { |
90 | 0 | apr_status_t stat = apr_file_close(m_priv->fileptr); |
91 | |
|
92 | 0 | if (stat == APR_SUCCESS) |
93 | 0 | { |
94 | 0 | m_priv->fileptr = NULL; |
95 | 0 | } |
96 | 0 | else |
97 | 0 | { |
98 | 0 | throw IOException(stat); |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | |
103 | | int FileInputStream::read(ByteBuffer& buf) |
104 | 0 | { |
105 | 0 | apr_size_t bytesRead = buf.remaining(); |
106 | 0 | apr_status_t stat = apr_file_read(m_priv->fileptr, buf.current(), &bytesRead); |
107 | 0 | int retval = -1; |
108 | |
|
109 | 0 | if (!APR_STATUS_IS_EOF(stat)) |
110 | 0 | { |
111 | 0 | if (stat != APR_SUCCESS) |
112 | 0 | { |
113 | 0 | throw IOException(stat); |
114 | 0 | } |
115 | | |
116 | 0 | buf.position(buf.position() + bytesRead); |
117 | 0 | retval = (int)bytesRead; |
118 | 0 | } |
119 | | |
120 | 0 | return retval; |
121 | 0 | } |