XMPPInputOutputStream.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 org.jivesoftware.smack.util.Objects;

  22. public abstract class XMPPInputOutputStream {

  23.     protected static FlushMethod flushMethod = FlushMethod.SYNC_FLUSH;

  24.     /**
  25.      * Set the used flushed method when compressing data. The default is full flush which may not
  26.      * achieve the best compression ratio, but provides better security against certain attacks.
  27.      * Only use sync flush if you fully understand the implications.
  28.      *
  29.      * @see <a href="https://blog.thijsalkema.de/blog/2014/08/07/https-attacks-and-xmpp-2-crime-and-breach/">Attacks against XMPP when using compression</a>
  30.      * @param flushMethod TODO javadoc me please
  31.      */
  32.     public static void setFlushMethod(FlushMethod flushMethod) {
  33.         XMPPInputOutputStream.flushMethod = Objects.requireNonNull(flushMethod);
  34.     }

  35.     public static FlushMethod getFlushMethod() {
  36.         return flushMethod;
  37.     }

  38.     protected final String compressionMethod;

  39.     protected XMPPInputOutputStream(String compressionMethod) {
  40.         this.compressionMethod = compressionMethod;
  41.     }

  42.     public String getCompressionMethod() {
  43.         return compressionMethod;
  44.     }

  45.     public abstract boolean isSupported();

  46.     public abstract InputStream getInputStream(InputStream inputStream) throws IOException;

  47.     public abstract OutputStream getOutputStream(OutputStream outputStream) throws IOException;

  48.     public enum FlushMethod {
  49.         FULL_FLUSH,
  50.         SYNC_FLUSH,
  51.     }
  52. }