/src/logging-log4cxx/src/main/cpp/fileinputstream.cpp
Line | Count | Source |
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 | | FileInputStreamPrivate(const File& aFile) |
31 | 1 | : path(aFile) |
32 | 1 | {} |
33 | | |
34 | | File path; |
35 | | Pool pool; |
36 | | apr_file_t* fileptr{ nullptr }; |
37 | | void open(); |
38 | | }; |
39 | | |
40 | | IMPLEMENT_LOG4CXX_OBJECT(FileInputStream) |
41 | | |
42 | | FileInputStream::FileInputStream(const LogString& filename) : |
43 | 0 | m_priv(std::make_unique<FileInputStreamPrivate>(filename)) |
44 | 0 | { |
45 | 0 | m_priv->open(); |
46 | 0 | } |
47 | | |
48 | | FileInputStream::FileInputStream(const logchar* filename) : |
49 | 0 | m_priv(std::make_unique<FileInputStreamPrivate>(filename)) |
50 | 0 | { |
51 | 0 | m_priv->open(); |
52 | 0 | } |
53 | | |
54 | | |
55 | | void FileInputStream::FileInputStreamPrivate::open() |
56 | 1 | { |
57 | 1 | apr_fileperms_t perm = APR_OS_DEFAULT; |
58 | 1 | apr_int32_t flags = APR_READ; |
59 | 1 | apr_status_t stat = apr_file_open(&this->fileptr, this->path.getAPRPath(), flags, perm, this->pool.getAPRPool()); |
60 | | |
61 | 1 | if (stat != APR_SUCCESS) |
62 | 0 | { |
63 | 0 | throw IOException(this->path.getAPRPath(), stat); |
64 | 0 | } |
65 | 1 | } |
66 | | |
67 | | |
68 | | FileInputStream::FileInputStream(const File& aFile) : |
69 | 1 | m_priv(std::make_unique<FileInputStreamPrivate>(aFile)) |
70 | 1 | { |
71 | 1 | m_priv->open(); |
72 | 1 | } |
73 | | |
74 | | |
75 | | FileInputStream::~FileInputStream() |
76 | 1 | { |
77 | 1 | if (m_priv->fileptr) |
78 | 1 | { |
79 | 1 | apr_file_close(m_priv->fileptr); |
80 | 1 | } |
81 | 1 | } |
82 | | |
83 | | |
84 | | void FileInputStream::close() |
85 | 0 | { |
86 | 0 | apr_status_t stat = apr_file_close(m_priv->fileptr); |
87 | |
|
88 | 0 | if (stat == APR_SUCCESS) |
89 | 0 | { |
90 | 0 | m_priv->fileptr = NULL; |
91 | 0 | } |
92 | 0 | else |
93 | 0 | { |
94 | 0 | throw IOException(stat); |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | |
99 | | int FileInputStream::read(ByteBuffer& buf) |
100 | 2 | { |
101 | 2 | apr_size_t bytesRead = buf.remaining(); |
102 | 2 | apr_status_t stat = apr_file_read(m_priv->fileptr, buf.current(), &bytesRead); |
103 | 2 | int retval = -1; |
104 | | |
105 | 2 | if (!APR_STATUS_IS_EOF(stat)) |
106 | 1 | { |
107 | 1 | if (stat != APR_SUCCESS) |
108 | 0 | { |
109 | 0 | throw IOException(stat); |
110 | 0 | } |
111 | | |
112 | 1 | buf.increment_position(bytesRead); |
113 | 1 | retval = (int)bytesRead; |
114 | 1 | } |
115 | | |
116 | 2 | return retval; |
117 | 2 | } |