文档章节

RedisOutputStream.java

强子大叔的码田
 强子大叔的码田
发布于 2014/03/13 02:35
字数 827
阅读 55
收藏 0
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[];//缓冲区

    protected int count;//计数器
    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 {//写到缓冲区,满时执行缓冲区清除
        buf[count++] = (byte) b;
        if (count == buf.length) {
            flushBuffer();
        }
    }

    public void write(byte b[], int off, int len) throws IOException {//写到缓冲区
        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 {//写到缓冲区
        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 {//写到缓冲区里
        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 {//写一个整数
        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 {//写完缓冲区,并且对out执行flush操作。
        flushBuffer();
        out.flush();
    }
}


© 著作权归作者所有

强子大叔的码田

强子大叔的码田

粉丝 908
博文 1429
码字总数 1219943
作品 9
南京
架构师
私信 提问
JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error at redis.clients.jedis.Connection.flush(C......

知行合一1
2016/07/20
1K
1
ArrayIndexOutOfBoundsException: 8440 at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java

java.lang.ArrayIndexOutOfBoundsException: 8440 at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:35) at redis.clients.jedis.Protocol.sendCommand(Protocol.java......

知行合一1
2016/07/20
563
1

没有更多内容

加载失败,请刷新页面

加载更多

没有更多内容

【0918】正则介绍_grep

【0918】正则介绍_grep 9.1 正则介绍_grep上 9.2 grep中 9.3 grep下 一、正则介绍 正则是一串有规律的字符串,它使用单个字符串来描述或匹配一系列符合某个语法规则的字符串。 二、grep工具 ...

飞翔的竹蜻蜓
31分钟前
4
0
为什么要在网站中应用CDN加速?

1. 网页加载速度更快 在网站中使用CDN技术最直接的一个好处就是它可以加快网页的加载速度。首先,CDN加速的内容分发是基于服务器缓存的,由于CDN中缓存了不少数据,它能够给用户提供更快的页...

云漫网络Ruan
今天
8
0
亚玛芬体育(Amer Sports)和信必优正式启动合作开发Movesense创新

亚玛芬体育和信必优正式启动合作开发Movesense创新,作为亚玛芬体育的完美技术搭档,信必优利用Movesense传感器技术为第三方开发移动应用和服务。 Movesense基于传感器技术和开放的API,测量...

symbiochina88
今天
4
0
创龙TI AM437x ARM Cortex-A9 + Xilinx Spartan-6 FPGA核心板规格书

SOM-TL437xF是一款广州创龙基于TI AM437x ARM Cortex-A9 + Xilinx Spartan-6 FPGA芯片设计的核心板,采用沉金无铅工艺的10层板设计,适用于高速数据采集和处理系统、汽车导航、工业自动化等领...

Tronlong创龙
今天
5
0
好程序员Java学习路线分享MyBatis之线程优化

  好程序员Java学习路线分享MyBatis之线程优化,我们的项目存在大量用户同时访问的情况,那么就会出现大量线程并发访问数据库,这样会带来线程同步问题,本章我们将讨论MyBatis的线程同步问...

好程序员官方
今天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部