natDeflater.cc 4.58 KB
Newer Older
1 2
// natDeflater.cc - Implementation of Deflater native methods.

3
/* Copyright (C) 1999, 2002  Free Software Foundation
4 5 6 7 8 9 10 11 12 13 14 15

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

// Written by Tom Tromey <tromey@cygnus.com>

#include <config.h>

#include <zlib.h>
16
#include <stdlib.h>
17

Tom Tromey committed
18
#include <gcj/cni.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <jvm.h>

#include <java/util/zip/Deflater.h>
#include <java/util/zip/DataFormatException.h>

#include <java/lang/InternalError.h>
#include <java/lang/NullPointerException.h>
#include <java/lang/ArrayIndexOutOfBoundsException.h>

extern void *_Jv_ZMalloc (void *, uInt nitems, uInt size);
extern void _Jv_ZFree (void *, void *addr);



jint
java::util::zip::Deflater::deflate (jbyteArray buf, jint off, jint len)
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;

  if (! buf)
40
    throw new java::lang::NullPointerException;
41
  if (off < 0 || len < 0 || off + len > buf->length)
42
    throw new java::lang::ArrayIndexOutOfBoundsException;
43

44 45 46
  if (len == 0)
    return 0;

47 48 49 50 51 52 53
  s->next_out = (Bytef *) (elements (buf) + off);
  s->avail_out = len;

  switch (::deflate (s, flush_flag))
    {
    case Z_STREAM_END:
      is_finished = true;
54
      if (s->avail_out == (unsigned int) len)
55 56 57 58 59 60
	return -1;
      break;

    case Z_STREAM_ERROR:
    case Z_BUF_ERROR:
      // FIXME?
61
      throw new java::lang::InternalError;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      break;

    case Z_OK:
      break;
    }

  return len - s->avail_out;
}

void
java::util::zip::Deflater::end ()
{
  JvSynchronize sync (this);
  // Just ignore errors.
  deflateEnd ((z_streamp) zstream);
  _Jv_Free (zstream);
  zstream = NULL;
}

void
java::util::zip::Deflater::finish ()
{
  JvSynchronize sync (this);
  flush_flag = Z_FINISH;
}

jint
java::util::zip::Deflater::getAdler ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  return s->adler;
}

jint
java::util::zip::Deflater::getTotalIn ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  return s->total_in;
}

jint
java::util::zip::Deflater::getTotalOut ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  return s->total_out;
}

jboolean
java::util::zip::Deflater::needsInput ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
117
  return s->avail_in == 0;
118 119 120 121 122 123 124 125 126
}

void
java::util::zip::Deflater::reset ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  // Just ignore errors.
  deflateReset (s);
127
  s->avail_in = 0;
128
  flush_flag = 0;
129
  is_finished = false;
130 131 132 133 134 135 136 137 138
}

void
java::util::zip::Deflater::setDictionary (jbyteArray buf, jint off, jint len)
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;

  if (! buf)
139
    throw new java::lang::NullPointerException;
140
  if (off < 0 || len < 0 || off + len > buf->length)
141
    throw new java::lang::ArrayIndexOutOfBoundsException;
142 143 144 145 146 147 148 149 150 151 152 153

  // Ignore errors.
  deflateSetDictionary (s, (Bytef *) (elements (buf) + off), len);
}

void
java::util::zip::Deflater::setInput (jbyteArray buf, jint off, jint len)
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;

  if (! buf)
154
    throw new java::lang::NullPointerException;
155
  if (off < 0 || len < 0 || off + len > buf->length)
156
    throw new java::lang::ArrayIndexOutOfBoundsException;
157 158 159 160 161 162 163 164 165 166 167

  s->next_in = (Bytef *) (elements (buf) + off);
  s->avail_in = len;
}

void
java::util::zip::Deflater::update ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;

168
  int strat = Z_DEFAULT_STRATEGY;
169 170 171 172 173 174 175 176 177 178 179
  switch (strategy)
    {
    case DEFAULT_STRATEGY:
      strat = Z_DEFAULT_STRATEGY;
      break;
    case FILTERED:
      strat = Z_FILTERED;
      break;
    case HUFFMAN_ONLY:
      strat = Z_HUFFMAN_ONLY;
      break;
180 181
    default:
      JvFail ("unexpected strategy");
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    }

  // Ignore errors.
  deflateParams (s, level, strat);
}

void
java::util::zip::Deflater::init (jint level, jboolean no_header)
{
  z_stream_s *stream = (z_stream_s *) _Jv_Malloc (sizeof (z_stream_s));
  stream->next_in = Z_NULL;
  stream->avail_in = 0;
  stream->zalloc = _Jv_ZMalloc;
  stream->zfree = _Jv_ZFree;
  stream->opaque = NULL;

  // Handle NO_HEADER using undocumented zlib feature.
  int wbits = MAX_WBITS;
  if (no_header)
    wbits = - wbits;

#define DEFAULT_MEM_LEVEL 8
  if (deflateInit2 (stream, level, Z_DEFLATED, wbits,
		    DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
    {
      jstring msg = NULL;
      if (stream->msg != NULL)
	msg = JvNewStringLatin1 (stream->msg);
210
      throw new java::lang::InternalError (msg);
211 212 213 214 215 216
    }

  zstream = reinterpret_cast<gnu::gcj::RawData *> (stream);
  is_finished = false;
  flush_flag = 0;
}