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