Java7ZlibInputOutputStream.java

  1. /**
  2.  *
  3.  * Copyright 2013-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * 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. package org.jivesoftware.smack.compression;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.util.zip.Deflater;
  22. import java.util.zip.DeflaterOutputStream;
  23. import java.util.zip.Inflater;
  24. import java.util.zip.InflaterInputStream;

  25. /**
  26.  * This class provides XMPP "zlib" compression with the help of the Deflater class of the Java API.
  27.  * Note that the method needed for compression with synchronous flush support is available since
  28.  * Java7, so it will only work with Java7 or higher (hence it's name). On Android, the required
  29.  * <code>deflate()</code> method is available on API 19 or higher.
  30.  * <p>
  31.  * See also:
  32.  * <ul>
  33.   * <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#deflate(byte[],%20int,%20int,%20int)">The required deflate() method (Java7)</a>
  34.  * <li><a href="http://developer.android.com/reference/java/util/zip/Deflater.html#deflate(byte[],%20int,%20int,%20int)">The required deflate() method (Android)</a>
  35.  * </ul>
  36.  *
  37.  * @author Florian Schmaus
  38.  */
  39. public class Java7ZlibInputOutputStream extends XMPPInputOutputStream {
  40.     private static final int compressionLevel = Deflater.DEFAULT_COMPRESSION;

  41.     public Java7ZlibInputOutputStream() {
  42.         super("zlib");
  43.     }

  44.     @Override
  45.     public boolean isSupported() {
  46.         return true;
  47.     }

  48.     @Override
  49.     public InputStream getInputStream(InputStream inputStream) {
  50.         return new InflaterInputStream(inputStream, new Inflater(), 512) {
  51.             /**
  52.              * Provide a more InputStream compatible version. A return value of 1 means that it is likely to read one
  53.              * byte without blocking, 0 means that the system is known to block for more input.
  54.              *
  55.              * @return 0 if no data is available, 1 otherwise
  56.              * @throws IOException if an I/O error occurred.
  57.              */
  58.             @Override
  59.             public int available() throws IOException {
  60.                 /*
  61.                  * aSmack related remark (where KXmlParser is used):
  62.                  * This is one of the funny code blocks. InflaterInputStream.available violates the contract of
  63.                  * InputStream.available, which breaks kXML2.
  64.                  *
  65.                  * I'm not sure who's to blame, oracle/sun for a broken api or the google guys for mixing a sun bug with
  66.                  * a xml reader that can't handle it....
  67.                  *
  68.                  * Anyway, this simple if breaks suns distorted reality, but helps to use the api as intended.
  69.                  */
  70.                 if (inf.needsInput()) {
  71.                     return 0;
  72.                 }
  73.                 return super.available();
  74.             }
  75.         };
  76.     }

  77.     @Override
  78.     public OutputStream getOutputStream(OutputStream outputStream) {
  79.         final int flushMethodInt;
  80.         switch (flushMethod) {
  81.         case SYNC_FLUSH:
  82.             flushMethodInt = Deflater.SYNC_FLUSH;
  83.             break;
  84.         case FULL_FLUSH:
  85.             flushMethodInt = Deflater.FULL_FLUSH;
  86.             break;
  87.         default:
  88.             throw new AssertionError();
  89.         }

  90.         return new DeflaterOutputStream(outputStream, new Deflater(compressionLevel)) {
  91.             @Override
  92.             public void flush() throws IOException {
  93.                 int count;
  94.                 while ((count = def.deflate(buf, 0, buf.length, flushMethodInt)) > 0) {
  95.                     out.write(buf, 0, count);
  96.                 }
  97.                 super.flush();
  98.             }
  99.         };
  100.     }

  101. }