溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

RedisOutputStream.java的源碼是什么

發(fā)布時間:2022-01-05 17:10:04 來源:億速云 閱讀:126 作者:iii 欄目:云計算

本篇內(nèi)容介紹了“RedisOutputStream.java的源碼是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

package redis.clients.util;

import java.io.*;
import java.nio.charset.Charset;

/**
 * The class implements a buffered output stream without synchronization
 * There are also special operations like in-place string encoding.
 * This stream fully ignore mark/reset and should not be used outside Jedis
 */
public final class RedisOutputStream extends FilterOutputStream {
    protected final byte buf[];//緩沖區(qū)

    protected int count;//計數(shù)器
    public static final Charset CHARSET = Charset.forName("UTF-8");//編碼方式

    public RedisOutputStream(OutputStream out) {//初始化
        this(out, 8192);
    }

    public RedisOutputStream(OutputStream out, int size) {//初始化
        super(out);
        if (size <= 0) {
            throw new IllegalArgumentException("Buffer size <= 0");
        }
        buf = new byte[size];
    }

    private void flushBuffer() throws IOException {//將所有字符都寫出去
        if (count > 0) {
            out.write(buf, 0, count);
            count = 0;
        }
    }

    public void write(int b) throws IOException {//寫到緩沖區(qū),滿時執(zhí)行緩沖區(qū)清除
        buf[count++] = (byte) b;
        if (count == buf.length) {
            flushBuffer();
        }
    }

    public void write(byte b[], int off, int len) throws IOException {//寫到緩沖區(qū)
        if (len >= buf.length) {
            flushBuffer();
            out.write(b, off, len);
        } else {
            if (len >= buf.length - count) {
                flushBuffer();
            }

            System.arraycopy(b, off, buf, count, len);
            count += len;
        }
    }

    public void writeAsciiCrLf(String in) throws IOException {//寫到緩沖區(qū)
        final int size = in.length();

        for (int i = 0; i != size; ++i) {
            buf[count++] = (byte) in.charAt(i);
            if (count == buf.length) {
                flushBuffer();
            }
        }

        writeCrLf();
    }

    public static boolean isSurrogate(char ch) {//XXX
        return ch >= Character.MIN_SURROGATE && ch <= Character.MAX_SURROGATE;
    }

    public static int utf8Length (String str) {//utf8編碼的長度
        int strLen = str.length(), utfLen = 0;
        for(int i = 0; i != strLen; ++i) {
            char c = str.charAt(i);
            if (c < 0x80) {
                utfLen++;
            } else if (c < 0x800) {
                utfLen += 2;
            } else if (isSurrogate(c)) {
                i++;
                utfLen += 4;
            } else {
                utfLen += 3;
            }
        }
        return utfLen;
    }

    public void writeCrLf() throws IOException {//寫到緩沖區(qū)里
        if (2 >= buf.length - count) {
            flushBuffer();
        }

        buf[count++] = '\r';
        buf[count++] = '\n';
    }

    public void writeUtf8CrLf(String str) throws IOException {//XXX
        int strLen = str.length();

        int i;
        for (i = 0; i < strLen; i++) {
            char c = str.charAt(i);
            if (!(c < 0x80)) break;
            buf[count++] = (byte) c;
            if(count == buf.length) {
                flushBuffer();
            }
        }

        for (; i < strLen; i++) {
            char c = str.charAt(i);
            if (c < 0x80) {
                buf[count++] = (byte) c;
                if(count == buf.length) {
                    flushBuffer();
                }
            } else if (c < 0x800) {
                if(2 < buf.length - count) {
                    flushBuffer();
                }
                buf[count++] = (byte)(0xc0 | (c >> 6));
                buf[count++] = (byte)(0x80 | (c & 0x3f));
            } else if (isSurrogate(c)) {
                if(4 < buf.length - count) {
                    flushBuffer();
                }
                int uc = Character.toCodePoint(c, str.charAt(i++));
                buf[count++] = ((byte)(0xf0 | ((uc >> 18))));
                buf[count++] = ((byte)(0x80 | ((uc >> 12) & 0x3f)));
                buf[count++] = ((byte)(0x80 | ((uc >> 6) & 0x3f)));
                buf[count++] = ((byte)(0x80 | (uc & 0x3f)));
            } else {
                if(3 < buf.length - count) {
                    flushBuffer();
                }
                buf[count++] =((byte)(0xe0 | ((c >> 12))));
                buf[count++] =((byte)(0x80 | ((c >> 6) & 0x3f)));
                buf[count++] =((byte)(0x80 | (c & 0x3f)));
            }
        }

        writeCrLf();
    }

    private final static int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE};

    private final static byte[] DigitTens = {
            '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
            '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
            '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
            '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
            '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
            '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
            '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
            '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
            '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
            '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
    };

    private final static byte[] DigitOnes = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    };

    private final static byte[] digits = {
            '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b',
            'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z'
    };

    public void writeIntCrLf(int value) throws IOException {//寫一個整數(shù)
        if (value < 0) {
            write('-');
            value = -value;
        }

        int size = 0;
        while (value > sizeTable[size])
            size++;

        size++;
        if (size >= buf.length - count) {
            flushBuffer();
        }

        int q, r;
        int charPos = count + size;

        while (value >= 65536) {
            q = value / 100;
            r = value - ((q << 6) + (q << 5) + (q << 2));
            value = q;
            buf[--charPos] = DigitOnes[r];
            buf[--charPos] = DigitTens[r];
        }

        for (; ;) {
            q = (value * 52429) >>> (16 + 3);
            r = value - ((q << 3) + (q << 1));
            buf[--charPos] = digits[r];
            value = q;
            if (value == 0) break;
        }
        count += size;

        writeCrLf();
    }

    public void flush() throws IOException {//寫完緩沖區(qū),并且對out執(zhí)行flush操作。
        flushBuffer();
        out.flush();
    }
}

“RedisOutputStream.java的源碼是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI