/src/logging-log4cxx/src/main/cpp/inputstreamreader.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/inputstreamreader.h> |
20 | | #include <log4cxx/helpers/exception.h> |
21 | | #include <log4cxx/helpers/pool.h> |
22 | | #include <log4cxx/helpers/bytebuffer.h> |
23 | | |
24 | | #include <cstdio> |
25 | | #include <cstring> |
26 | | |
27 | | using namespace LOG4CXX_NS; |
28 | | using namespace LOG4CXX_NS::helpers; |
29 | | |
30 | | IMPLEMENT_LOG4CXX_OBJECT(InputStreamReader) |
31 | | |
32 | | struct InputStreamReader::InputStreamReaderPrivate{ |
33 | | InputStreamReaderPrivate(const InputStreamPtr& in1) : |
34 | 0 | in(in1), dec(CharsetDecoder::getDefaultDecoder()){} |
35 | | |
36 | | InputStreamReaderPrivate(const InputStreamPtr& in1, const CharsetDecoderPtr& dec1) : |
37 | 0 | in(in1), dec(dec1) {} |
38 | | |
39 | | InputStreamPtr in; |
40 | | CharsetDecoderPtr dec; |
41 | | }; |
42 | | |
43 | | InputStreamReader::InputStreamReader(const InputStreamPtr& in1) |
44 | 0 | : m_priv(std::make_unique<InputStreamReaderPrivate>(in1)) |
45 | 0 | { |
46 | 0 | if (in1 == 0) |
47 | 0 | { |
48 | 0 | throw NullPointerException(LOG4CXX_STR("in parameter may not be null.")); |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | InputStreamReader::InputStreamReader(const InputStreamPtr& in1, const CharsetDecoderPtr& dec1) |
53 | 0 | : m_priv(std::make_unique<InputStreamReaderPrivate>(in1, dec1)) |
54 | 0 | { |
55 | 0 | if (in1 == 0) |
56 | 0 | { |
57 | 0 | throw NullPointerException(LOG4CXX_STR("in parameter may not be null.")); |
58 | 0 | } |
59 | | |
60 | 0 | if (dec1 == 0) |
61 | 0 | { |
62 | 0 | throw NullPointerException(LOG4CXX_STR("dec parameter may not be null.")); |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | InputStreamReader::~InputStreamReader() |
67 | 0 | { |
68 | 0 | } |
69 | | |
70 | | void InputStreamReader::close(Pool& ) |
71 | 0 | { |
72 | 0 | m_priv->in->close(); |
73 | 0 | } |
74 | | |
75 | | LogString InputStreamReader::read(Pool& p) |
76 | 0 | { |
77 | 0 | const size_t BUFSIZE = 4096; |
78 | 0 | ByteBuffer buf(p.pstralloc(BUFSIZE), BUFSIZE); |
79 | 0 | LogString output; |
80 | | |
81 | | // read whole file |
82 | 0 | while (m_priv->in->read(buf) >= 0) |
83 | 0 | { |
84 | 0 | buf.flip(); |
85 | 0 | log4cxx_status_t stat = m_priv->dec->decode(buf, output); |
86 | |
|
87 | 0 | if (stat != 0) |
88 | 0 | { |
89 | 0 | throw IOException(LOG4CXX_STR("decode"), stat); |
90 | 0 | } |
91 | | |
92 | 0 | if (buf.remaining() > 0) |
93 | 0 | { |
94 | 0 | memmove(buf.data(), buf.current(), buf.remaining()); |
95 | 0 | buf.limit(buf.remaining()); |
96 | 0 | } |
97 | 0 | else |
98 | 0 | { |
99 | 0 | buf.clear(); |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | 0 | return output; |
104 | 0 | } |