1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hiodev.cpp,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 /* $Id: hiodev.cpp,v 1.6 2008-04-10 12:05:38 rt Exp $ */
34 #pragma implementation "hiodev.h"
39 // DVO: add zlib/ prefix
43 #include <zlib/zlib.h>
57 const int BUFSIZE
= 1024;
58 static uchar rBuf
[BUFSIZE
];
60 // HIODev abstract class
78 int HIODev::read1b(void *ptr
, int nmemb
)
80 uchar
*p
= (uchar
*) ptr
;
85 for (ii
= 0; ii
< nmemb
; ii
++)
87 p
[ii
] = sal::static_int_cast
<uchar
>(read1b());
95 int HIODev::read2b(void *ptr
, int nmemb
)
97 ushort
*p
= (ushort
*) ptr
;
102 for (ii
= 0; ii
< nmemb
; ii
++)
104 p
[ii
] = sal::static_int_cast
<uchar
>(read2b());
112 int HIODev::read4b(void *ptr
, int nmemb
)
114 ulong
*p
= (ulong
*) ptr
;
119 for (ii
= 0; ii
< nmemb
; ii
++)
130 HStreamIODev::HStreamIODev(HStream
& stream
):_stream(stream
)
136 HStreamIODev::~HStreamIODev()
142 void HStreamIODev::init()
149 bool HStreamIODev::open()
151 if (!(_stream
.available()))
157 void HStreamIODev::flush(void)
160 gz_flush(_gzfp
, Z_FINISH
);
164 void HStreamIODev::close(void)
166 /* Ç÷¯½ÃÇÑ ÈÄ ´Ý´Â´Ù. */
169 gz_close(_gzfp
); /* gz_close() calls stream_closeInput() */
171 _stream
.closeInput();
176 int HStreamIODev::state(void) const
183 bool HStreamIODev::setCompressed(bool flag
)
187 return 0 != (_gzfp
= gz_open(_stream
));
190 gz_flush(_gzfp
, Z_FINISH
);
200 #define GZREAD(ptr,len) (_gzfp?gz_read(_gzfp,ptr,len):0)
202 int HStreamIODev::read1b()
204 int res
= (compressed
) ? GZREAD(rBuf
, 1) : _stream
.readBytes(rBuf
, 1);
209 return (unsigned char) rBuf
[0];
213 int HStreamIODev::read2b()
215 int res
= (compressed
) ? GZREAD(rBuf
, 2) : _stream
.readBytes(rBuf
, 2);
220 return ((unsigned char) rBuf
[1] << 8 | (unsigned char) rBuf
[0]);
224 long HStreamIODev::read4b()
226 int res
= (compressed
) ? GZREAD(rBuf
, 4) : _stream
.readBytes(rBuf
, 4);
231 return ((unsigned char) rBuf
[3] << 24 | (unsigned char) rBuf
[2] << 16 |
232 (unsigned char) rBuf
[1] << 8 | (unsigned char) rBuf
[0]);
236 int HStreamIODev::readBlock(void *ptr
, int size
)
239 (compressed
) ? GZREAD(ptr
, size
) : _stream
.readBytes((byte
*) ptr
,
247 int HStreamIODev::skipBlock(int size
)
250 if( size
<= BUFSIZE
)
251 return GZREAD(rBuf
, size
);
255 if( remain
> BUFSIZE
)
256 remain
-= GZREAD(rBuf
, BUFSIZE
);
258 remain
-= GZREAD(rBuf
, remain
);
262 return size
- remain
;
265 return _stream
.skipBytes(size
);
269 HMemIODev::HMemIODev(char *s
, int len
)
277 HMemIODev::~HMemIODev()
283 void HMemIODev::init()
291 bool HMemIODev::open()
297 void HMemIODev::flush(void)
302 void HMemIODev::close(void)
307 int HMemIODev::state(void) const
316 bool HMemIODev::setCompressed(bool )
322 int HMemIODev::read1b()
331 int HMemIODev::read2b()
335 return ptr
[pos
- 1] << 8 | ptr
[pos
- 2];
341 long HMemIODev::read4b()
345 return DWORD(ptr
[pos
- 1] << 24 | ptr
[pos
- 2] << 16 |
346 ptr
[pos
- 3] << 8 | ptr
[pos
- 4]);
352 int HMemIODev::readBlock(void *p
, int size
)
354 if (length
< pos
+ size
)
356 memcpy(p
, ptr
+ pos
, size
);
362 int HMemIODev::skipBlock(int size
)
364 if (length
< pos
+ size
)