Home | History | Annotate | Download | only in sevenz
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  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  */
     18 package org.apache.commons.compress.archivers.sevenz;
     19 
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 import java.io.OutputStream;
     23 
     24 /**
     25  * Base Codec class.
     26  */
     27 abstract class CoderBase {
     28     private final Class<?>[] acceptableOptions;
     29     private static final byte[] NONE = new byte[0];
     30 
     31     /**
     32      * @param acceptableOptions types that can be used as options for this codec.
     33      */
     34     protected CoderBase(final Class<?>... acceptableOptions) {
     35         this.acceptableOptions = acceptableOptions;
     36     }
     37 
     38     /**
     39      * @return whether this method can extract options from the given object.
     40      */
     41     boolean canAcceptOptions(final Object opts) {
     42         for (final Class<?> c : acceptableOptions) {
     43             if (c.isInstance(opts)) {
     44                 return true;
     45             }
     46         }
     47         return false;
     48     }
     49 
     50     /**
     51      * @return property-bytes to write in a Folder block
     52      */
     53     byte[] getOptionsAsProperties(final Object options) throws IOException {
     54         return NONE;
     55     }
     56 
     57     /**
     58      * @return configuration options that have been used to create the given InputStream from the given Coder
     59      */
     60     Object getOptionsFromCoder(final Coder coder, final InputStream in) throws IOException {
     61         return null;
     62     }
     63 
     64     /**
     65      * @return a stream that reads from in using the configured coder and password.
     66      */
     67     abstract InputStream decode(final String archiveName,
     68         final InputStream in, long uncomressedLength,
     69         final Coder coder, byte[] password) throws IOException;
     70 
     71     /**
     72      * @return a stream that writes to out using the given configuration.
     73      */
     74     OutputStream encode(final OutputStream out, final Object options) throws IOException {
     75         throw new UnsupportedOperationException("method doesn't support writing");
     76     }
     77 
     78     /**
     79      * If the option represents a number, return its integer
     80      * value, otherwise return the given default value.
     81      */
     82     protected static int numberOptionOrDefault(final Object options, final int defaultValue) {
     83         return options instanceof Number ? ((Number) options).intValue() : defaultValue;
     84     }
     85 }
     86