Java Byte Array to String by Charset inflate(byte[] bytes, Charset encoding)

Here you can find the source of inflate(byte[] bytes, Charset encoding)

Description

inflate

License

Open Source License

Declaration

public static final String inflate(byte[] bytes, Charset encoding)
            throws DataFormatException, UnsupportedEncodingException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.zip.DataFormatException;

import java.util.zip.Inflater;

public class Main {
    private static final int MAX_THREAD_BUFFERS = 100;
    private static final int THREAD_BUFFER_LENGTH = 1000;
    private static final Object _lock = new Object();
    private static final Map<Long, byte[]> _threadBuffers = new HashMap<>();
    private static final Queue<Long> _threadQueue = new LinkedList<>();

    public static final String inflate(byte[] bytes, Charset encoding)
            throws DataFormatException, UnsupportedEncodingException {

        // create inflater
        Inflater inf = new Inflater(true);
        inf.reset();//  w  w  w  . jav a  2 s .c o  m
        inf.setInput(bytes);

        // get thread buffer
        byte[] buffer = getThreadBuffer();

        // inflate and write output
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (!inf.finished()) {
            int n = inf.inflate(buffer);
            baos.write(buffer, 0, n);
        }

        // get result and return
        byte[] bytes2 = baos.toByteArray();
        String str = new String(bytes2, encoding);
        return str;
    }

    private static final byte[] getThreadBuffer() {

        byte[] buffer;
        synchronized (_lock) {

            // get thread buffer
            Long threadId = Thread.currentThread().getId();
            buffer = _threadBuffers.get(threadId);
            if (buffer == null) {
                buffer = new byte[THREAD_BUFFER_LENGTH];
                _threadBuffers.put(threadId, buffer);
                _threadQueue.add(threadId);
            }

            // throw away old buffers
            while (_threadQueue.size() > MAX_THREAD_BUFFERS) {
                Long removeThreadId = _threadQueue.poll();
                _threadBuffers.remove(removeThreadId);
            }
        }

        return buffer;
    }
}

Related

  1. getRowId(final byte[] rowId, final Charset charset, final boolean base64encodeValues)
  2. getString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset, @Nonnull Charset charset)
  3. getString(byte[] bytes, String charset)
  4. getString(byte[] bytesIn, Charset cs)
  5. identify(byte[] bytes, CharsetDecoder decoder)
  6. isCharset(byte[] b, String inCharset)
  7. newString(byte[] bytes, Charset charset)
  8. newString(byte[] bytes, String charsetName)
  9. newString(final byte[] bytes, final Charset charset)