All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.fastjson.util.ThreadLocalCache Maven / Gradle / Ivy

There is a newer version: 3.2.26
Show newest version
/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at  http://www.gnu.org/licenses/lgpl-3.0.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.fastjson.util;

import java.lang.ref.SoftReference;
import java.nio.charset.CharsetDecoder;

/**
 * @deprecated
 */
public class ThreadLocalCache {

    public final static int CHARS_CACH_INIT_SIZE = 1024;                                    // 1k, 2^10;
    public final static int CHARS_CACH_INIT_SIZE_EXP = 10;
    public final static int CHARS_CACH_MAX_SIZE = 1024 * 128;                              // 128k, 2^17;
    public final static int CHARS_CACH_MAX_SIZE_EXP = 17;
    private final static ThreadLocal> charsBufLocal = new ThreadLocal>();

    private final static ThreadLocal decoderLocal = new ThreadLocal();

    public static CharsetDecoder getUTF8Decoder() {
        CharsetDecoder decoder = decoderLocal.get();
        if (decoder == null) {
            decoder = new UTF8Decoder();
            decoderLocal.set(decoder);
        }
        return decoder;
    }

    public static void clearChars() {
        charsBufLocal.set(null);
    }

    public static char[] getChars(int length) {
        SoftReference ref = charsBufLocal.get();

        if (ref == null) {
            return allocate(length);
        }

        char[] chars = ref.get();

        if (chars == null) {
            return allocate(length);
        }

        if (chars.length < length) {
            chars = allocate(length);
        }

        return chars;
    }

    private static char[] allocate(int length) {
        if (length > CHARS_CACH_MAX_SIZE) {
            return new char[length];
        }

        int allocateLength = getAllocateLengthExp(CHARS_CACH_INIT_SIZE_EXP, CHARS_CACH_MAX_SIZE_EXP, length);
        char[] chars = new char[allocateLength];
        charsBufLocal.set(new SoftReference(chars));
        return chars;
    }

    private static int getAllocateLengthExp(int minExp, int maxExp, int length) {
        assert (1 << maxExp) >= length;
//		int max = 1 << maxExp;
//		if(length>= max) {
//			return length;
//		}
        int part = length >>> minExp;
        if (part <= 0) {
            return 1 << minExp;
        }
        return 1 << 32 - Integer.numberOfLeadingZeros(length - 1);
    }

    // /////////
    public final static int BYTES_CACH_INIT_SIZE = 1024;                                    // 1k, 2^10;
    public final static int BYTES_CACH_INIT_SIZE_EXP = 10;
    public final static int BYTES_CACH_MAX_SIZE = 1024 * 128;                              // 128k, 2^17;
    public final static int BYTES_CACH_MAX_SIZE_EXP = 17;
    private final static ThreadLocal> bytesBufLocal = new ThreadLocal>();

    public static void clearBytes() {
        bytesBufLocal.set(null);
    }

    public static byte[] getBytes(int length) {
        SoftReference ref = bytesBufLocal.get();

        if (ref == null) {
            return allocateBytes(length);
        }

        byte[] bytes = ref.get();

        if (bytes == null) {
            return allocateBytes(length);
        }

        if (bytes.length < length) {
            bytes = allocateBytes(length);
        }

        return bytes;
    }

    private static byte[] allocateBytes(int length) {
        if (length > BYTES_CACH_MAX_SIZE) {
            return new byte[length];
        }

        int allocateLength = getAllocateLengthExp(BYTES_CACH_INIT_SIZE_EXP, BYTES_CACH_MAX_SIZE_EXP, length);
        byte[] chars = new byte[allocateLength];
        bytesBufLocal.set(new SoftReference(chars));
        return chars;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy