1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Jos van den Oever
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
#include "CLucene/_ApiHeader.h"
8
#include "gzipinputstream.h"
9
#include "CLucene/util/_bufferedstream.h"
15
class GZipInputStream::Internal{
17
class JStreamsBuffer: public BufferedInputStreamImpl{
19
BufferedInputStream* input;
22
int32_t fillBuffer(signed char* start, int32_t space) {
23
if (zstream == 0) return -1;
24
// make sure there is data to decompress
25
if (zstream->avail_out) {
26
// read data from the input stream
27
const signed char* inStart;
28
int32_t nread = input->read(inStart, 1, 0);
30
_CLTHROWA(CL_ERR_IO, "unexpected end of stream");
32
zstream->next_in = (Bytef*)inStart;
33
zstream->avail_in = nread;
36
// make sure we can write into the buffer
37
zstream->avail_out = space;
38
zstream->next_out = (Bytef*)start;
40
int r = inflate(zstream, Z_SYNC_FLUSH);
41
// inform the buffer of the number of bytes that was read
42
int32_t nwritten = space - zstream->avail_out;
45
_CLTHROWA(CL_ERR_IO, "Z_NEED_DICT while inflating stream.");
48
_CLTHROWA(CL_ERR_IO, "Z_DATA_ERROR while inflating stream.");
51
_CLTHROWA(CL_ERR_IO, "Z_MEM_ERROR while inflating stream.");
54
if (zstream->avail_in) {
55
input->reset(input->position()-zstream->avail_in);
57
// we are finished decompressing,
58
// (but this stream is not yet finished)
71
const unsigned char* buf;
72
const signed char* begin;
75
int64_t pos = input->position();
76
nread = input->read(begin, 2, 2);
82
buf = (const unsigned char*)begin;
83
return buf[0] == 0x1f && buf[1] == 0x8b;
89
JStreamsBuffer(BufferedInputStream* input, GZipInputStream::ZipFormat format){
92
// check first bytes of stream before allocating buffer
93
if (format == GZipInputStream::GZIPFORMAT && !checkMagic()) {
94
_CLTHROWA(CL_ERR_IO, "Magic bytes are wrong.");
97
// initialize the z_stream
98
zstream = (z_stream_s*)malloc(sizeof(z_stream_s));
99
zstream->zalloc = Z_NULL;
100
zstream->zfree = Z_NULL;
101
zstream->opaque = Z_NULL;
102
zstream->avail_in = 0;
103
zstream->next_in = Z_NULL;
104
// initialize for reading gzip streams
105
// for reading libz streams, you need inflateInit(zstream)
108
case GZipInputStream::ZLIBFORMAT:
109
r = inflateInit(zstream);
111
case GZipInputStream::GZIPFORMAT:
112
r = inflateInit2(zstream, 15+16);
114
case GZipInputStream::ZIPFORMAT:
116
r = inflateInit2(zstream, -MAX_WBITS);
121
_CLTHROWA(CL_ERR_IO, "Error initializing GZipInputStream.");
124
// signal that we need to read into the buffer
125
zstream->avail_out = 1;
127
void _setMinBufSize(int32_t bufsize){
128
this->setMinBufSize(bufsize);
136
JStreamsBuffer* jsbuffer;
138
Internal(BufferedInputStream* input, GZipInputStream::ZipFormat format){
139
jsbuffer = new JStreamsBuffer(input, format);
146
GZipInputStream::GZipInputStream(InputStream* input, ZipFormat format) {
147
internal = new Internal(_CLNEW FilteredBufferedInputStream(input, false), format);
149
GZipInputStream::GZipInputStream(BufferedInputStream* input, ZipFormat format) {
150
internal = new Internal(input, format);
153
GZipInputStream::~GZipInputStream() {
158
int32_t GZipInputStream::read(const signed char*& start, int32_t min, int32_t max){
159
return internal->jsbuffer->read(start,min,max);
161
int32_t GZipInputStream::read(const unsigned char*& _start, int32_t min, int32_t max){
162
const signed char* start = 0;
163
int32_t ret = internal->jsbuffer->read(start,min,max);
164
_start = (const unsigned char*)start;
167
int64_t GZipInputStream::position(){
168
return internal->jsbuffer->position();
170
int64_t GZipInputStream::reset(int64_t to){
171
return internal->jsbuffer->reset(to);
173
int64_t GZipInputStream::skip(int64_t ntoskip){
174
return internal->jsbuffer->skip(ntoskip);
176
void GZipInputStream::setMinBufSize(int32_t minbufsize){
177
internal->jsbuffer->_setMinBufSize(minbufsize);
179
size_t GZipInputStream::size(){
180
return internal->jsbuffer->size();