DCL 4.0
Loading...
Searching...
No Matches
InputStreamReader Class Reference

#include <InputStreamReader.h>

Inheritance diagram for InputStreamReader:
Reader Object FileReader

Public Member Functions

virtual String toString () const
 InputStreamReader (InputStream &__noclose__ _input, CharsetDecoder &_decoder)
 InputStreamReader (InputStream *__destroy__ _pInput, CharsetDecoder *__destroy__ _pDecoder=NULL) __DCL_THROWS1(IOException *)
virtual ~InputStreamReader ()
virtual void close () __DCL_THROWS1(IOException *)
virtual size_t read (wchar_t *_buf, size_t _n) __DCL_THROWS1(IOException *)
Public Member Functions inherited from Object
virtual void destroy ()
String className () const
bool isInstanceOf (const std::type_info &typeinfo) const
virtual const std::type_info & typeInfo () const

Protected Attributes

InputStream__input
CharsetDecoder__decoder
bool __closeDestroy

Additional Inherited Members

Protected Member Functions inherited from Object
virtual ~Object ()
 Object ()

Detailed Description

Definition at line 25 of file InputStreamReader.h.

Constructor & Destructor Documentation

◆ InputStreamReader() [1/2]

InputStreamReader::InputStreamReader ( InputStream &__noclose__ _input,
CharsetDecoder & _decoder )

참조된 스트림과 디코더로 객체를 구성한다. _input과 _decoder는 객체의 생명주기동안 유효해야 하며, _input은 close()에서 close하지 않는다.

Parameters
_inputA InputStream
_decoderA CharsetDecoder

Definition at line 47 of file InputStreamReader.cpp.

51{
52 __input = &_input;
53 __decoder = &_decoder;
54 __closeDestroy = false;
55 __extraBytes = 0;
56}
CharsetDecoder * __decoder
InputStream * __input

◆ InputStreamReader() [2/2]

InputStreamReader::InputStreamReader ( InputStream *__destroy__ _pInput,
CharsetDecoder *__destroy__ _pDecoder = NULL )

넘겨받은 스트림과 디코더로 객체를 구성한다. close()에서 _pInput의 close()가 호출되고 pInput과 _pDecoder는 파괴된다.

Parameters
_pInputA InputStream
_pDecoderNULL이면 처음 읽는 2~4바이트를 사용하여 BOM을 검사 한 후, 확인이 안되면 LocaleEncoder를 사용한다.

Definition at line 58 of file InputStreamReader.cpp.

62{
63 __DCL_ASSERT_PARAM(_pInput != NULL);
64
65 __input = _pInput;
66 __decoder = _pDecoder;
67 __closeDestroy = true;
68 __extraBytes = 0;
69}
#define NULL
Definition Config.h:340
#define __DCL_ASSERT_PARAM(expr)
Definition Object.h:384

◆ ~InputStreamReader()

InputStreamReader::~InputStreamReader ( )
virtual

객체의 파괴를 수행한다. close하지 않았으면 close()하며 예외가 발생하면 무시한다.

Definition at line 71 of file InputStreamReader.cpp.

72{
73 if (__input)
74 {
75 try
76 {
77 close();
78 }
79 catch (Exception* e)
80 {
81 __DCL_TRACE1(L"%ls\n", e->toString().data());
82 e->destroy();
83 }
84 }
85}
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
virtual String toString() const
Definition Exception.cpp:40
virtual void destroy()
Definition Exception.cpp:74
virtual void close() __DCL_THROWS1(IOException *)

Member Function Documentation

◆ close()

void InputStreamReader::close ( )
virtual

객체를 닫는다. 넘겨받은 InputStream과 CharsetDecoder는 파괴된다.

Definition at line 87 of file InputStreamReader.cpp.

89{
91
92 Exception* e = NULL;
93 InputStream* input = __input;
94 CharsetDecoder* decoder = __decoder;
95 __input = NULL;
97 __extraBytes = 0;
98
100 {
101 if (decoder)
102 decoder->destroy();
103
104 try
105 {
106 input->close();
107 }
108 catch (Exception* cause)
109 {
110 e = cause;
111 }
112 input->destroy();
113 }
114
115 if (e)
116 throw e;
117}
#define __DCL_ASSERT_HANDLE(expr)
Definition Object.h:383
virtual void destroy()
Definition Object.cpp:192

◆ read()

size_t InputStreamReader::read ( wchar_t * _buf,
size_t _n )
virtual

텍스트를 읽는다.

Parameters
_buf버퍼
_n_buf의 길이, _buf가 가리키는 버퍼의 wchar_t의 개수

Implements Reader.

Definition at line 119 of file InputStreamReader.cpp.

121{
123 __DCL_ASSERT_PARAM(_buf != NULL);
124
125 if (!_n)
126 return 0;
127
128 if (!__decoder)
129 {
130 char bom[4];
131 if (__input->available() >= sizeof(bom))
132 {
133 size_t readCount = __input->read(bom, sizeof(bom));
134 if ((readCount == 4) && IS_UTF32(bom))
135 __decoder = new UTF32Decoder();
136 else if ((readCount >= 3) && IS_UTF8(bom))
137 __decoder = new UTF8Decoder();
138 else if ((readCount >= 2) && IS_UTF16(bom))
139 __decoder = new UTF16Decoder();
140
141 memcpy(__extra, bom, readCount);
142 __extraBytes = readCount;
143 }
144
145 if (!__decoder)
146 __decoder = new LocaleDecoder();
147 }
148
149 wchar_t* outCur = _buf;
150 wchar_t* outEnd = _buf + _n;
151
152#define __BUFFER_SIZE__ 1024
154 size_t extraCount = __extraBytes;
155
156 // Non-Block이 가능한 양 만큼
157 size_t availCount = __input->available();
158 memcpy(inBuf, __extra, extraCount);
159
160 size_t outCount;
161 while ((outCount = outEnd - outCur) && availCount)
162 {
163 size_t readCount = __MIN(__MAX(outCount, __EXTRA_MAX), availCount, __BUFFER_SIZE__);
164 readCount = __input->read(inBuf + extraCount, readCount);
165 availCount -= readCount;
166
167 size_t inCount = readCount + extraCount;
168 if (!inCount)
169 break;
170
171 size_t inCountSave = inCount;
172 int r = __decoder->decode(inBuf, inCount, outCur, outCount);
173 /*__DCL_TRACE4(__T("%ls read[%d], extra[%d], out[%d]\n"),
174 __decoder->className(), readCount, extraCount, outCount);*/
175 extraCount = inCountSave - inCount;
176 switch (r)
177 {
178 case CS_SOURCE_FEW :
179 __DCL_ASSERT(extraCount <= __EXTRA_MAX);
180 if (inCount > 0)
181 memmove(inBuf, inBuf + inCount, extraCount);
182 break;
183 case CS_NOERROR :
184 __DCL_ASSERT(extraCount == 0);
185 break;
186 default :
187 throw(new IOException(toString(), new CharsetConvertException(r)));
188 }
189 outCur += outCount;
190 }
191
192 // 이곳에서 Block 될 수 있다.
193 if ((outCount = outEnd - outCur) > 0)
194 {
195 size_t readCount = __MIN(__MAX(outCount, __EXTRA_MAX), __BUFFER_SIZE__);
196 readCount = __input->read(inBuf + extraCount, readCount);
197 size_t inCount = readCount + extraCount;
198
199 if (inCount)
200 {
201 size_t inCountSave = inCount;
202 int r = __decoder->decode(inBuf, inCount, outCur, outCount);
203 /*__DCL_TRACE4(__T("%ls read[%d], extra[%d], out[%d]\n"),
204 __decoder->className(), readCount, extraCount, outCount);*/
205 extraCount = inCountSave - inCount;
206 switch (r)
207 {
208 case CS_SOURCE_FEW :
209 __DCL_ASSERT(extraCount <= __EXTRA_MAX);
210 if (inCount > 0)
211 memmove(inBuf, inBuf + inCount, extraCount);
212 break;
213 case CS_NOERROR :
214 __DCL_ASSERT(extraCount == 0);
215 break;
216 default :
217 throw(new IOException(className(), new CharsetConvertException(r)));
218 }
219 outCur += outCount;
220 }
221 }
222
223 memcpy(__extra, inBuf, extraCount);
224 __extraBytes = extraCount;
225
226 return outCur - _buf;
227}
#define IS_UTF16(bom)
Definition Charset.h:36
#define IS_UTF8(bom)
Definition Charset.h:33
#define IS_UTF32(bom)
Definition Charset.h:41
@ CS_SOURCE_FEW
Definition Charset.h:65
@ CS_NOERROR
Definition Charset.h:61
unsigned char byte_t
Definition Config.h:274
#define __BUFFER_SIZE__
#define __EXTRA_MAX
#define __DCL_ASSERT(expr)
Definition Object.h:371
ByteString r
virtual String toString() const
String className() const
Definition Object.cpp:163
size_t __MAX(size_t x, size_t y)
Definition size_t.h:43
size_t __MIN(size_t x, size_t y)
Definition size_t.h:27

◆ toString()

virtual String InputStreamReader::toString ( ) const
virtual

파생클래스를 위한 생성자이다. 객체의 구성에 대하여 간략한 요약을 리턴한다.

Reimplemented from Object.

Member Data Documentation

◆ __closeDestroy

bool InputStreamReader::__closeDestroy
protected

Definition at line 95 of file InputStreamReader.h.

◆ __decoder

CharsetDecoder* InputStreamReader::__decoder
protected

Definition at line 94 of file InputStreamReader.h.

◆ __input

InputStream* InputStreamReader::__input
protected

Definition at line 93 of file InputStreamReader.h.


The documentation for this class was generated from the following files: