Java InputStream Read by Charset createZipInputStream(InputStream inStream, Charset charset)

Here you can find the source of createZipInputStream(InputStream inStream, Charset charset)

Description

Returns a ZipInputStream opened with a given charset.

License

Apache License

Declaration

static ZipInputStream createZipInputStream(InputStream inStream, Charset charset) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.InputStream;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;

import java.util.zip.ZipInputStream;

public class Main {
    private static final String MISSING_METHOD_PLEASE_UPGRADE = "Your JRE doesn't support the ZipFile Charset constructor. Please upgrade JRE to 1.7 use this feature. Tried constructor ZipFile(File, Charset).";
    private static final String CONSTRUCTOR_MESSAGE_FOR_INPUT = "Using constructor ZipInputStream(InputStream, Charset) has failed: ";

    /**/*from  w w w. j  a v a2s .  c o  m*/
     * Returns a ZipInputStream opened with a given charset.
     */
    static ZipInputStream createZipInputStream(InputStream inStream, Charset charset) {
        if (charset == null)
            return new ZipInputStream(inStream);

        try {
            Constructor<ZipInputStream> constructor = ZipInputStream.class
                    .getConstructor(new Class[] { InputStream.class, Charset.class });
            return (ZipInputStream) constructor.newInstance(new Object[] { inStream, charset });
        } catch (NoSuchMethodException e) {
            throw new IllegalStateException(MISSING_METHOD_PLEASE_UPGRADE, e);
        } catch (InstantiationException e) {
            throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_INPUT + e.getMessage(), e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_INPUT + e.getMessage(), e);
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_INPUT + e.getMessage(), e);
        } catch (InvocationTargetException e) {
            throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_INPUT + e.getMessage(), e);
        }
    }
}

Related

  1. convertFromUnicode(String input, String targetCharset)
  2. convertStreamToString(final InputStream is, final Charset charset)
  3. convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet)
  4. copyToString(InputStream in, Charset charset)
  5. createInput(String s, String charsetName)
  6. forceEncoding(String inputString, String targetCharset)
  7. fromUnicode(String charset, String input)
  8. getContentFromInputStream(InputStream in, String charset)
  9. getHeaderLen(FileInputStream in, Charset encoding)