当前位置:首页>学习笔记>Netty学习笔记01、—(三大组件、bytebuffer、文件编程)

Netty学习笔记01、—(三大组件、bytebuffer、文件编程)

  • 2026-05-06 18:18:12
Netty学习笔记01、—(三大组件、bytebuffer、文件编程)

点击上方蓝字关注我们

一、三大组件

1.1、Channel & Buffer

Channel:数据的传输通道,双向通道,即可输入也可以输出

Buffer:输入数据需要临时存储在内存中,它就是一个内存缓冲区,暂存channel读入的数据,同样写出数据也是需要先写到buffer,应用程序与网络之间的桥梁,用于暂存数据的缓冲区

Stream:以前的输入输出流也是数据的传输通道。

channel 有一点类似于 stream,它就是读写数据的双向通道,可以从 channel 将数据读入 buffer,也可以将 buffer 的数据写入 channel,而之前的 stream 要么是输入,要么是输出,channel 比 stream 更为底层

channel
buffer

常见的 Channel 有

  • • FileChannel:文件的数据传输通道
  • • DatagramChannel:UDP网络传输通道
  • • SocketChannel:TCP数据传输通道,客户端/服务端都OK
  • • ServerSocketChannel:TCP,专用服务端

buffer 则用来缓冲读写数据,常见的 buffer 有

  • • ByteBuffer(最常用):以字节为单位缓冲数据。下面是实现类
    • • MappedByteBuffer
    • • DirectByteBuffer
    • • HeapByteBuffer
  • • 不同数据类型:
    • • ShortBuffer
    • • IntBuffer
    • • LongBuffer
    • • FloatBuffer
    • • DoubleBuffer
    • • CharBuffer

1.2、Selector

selector 单从字面意思不好理解,需要结合服务器的设计演化来理解它的用途

多线程版设计

多线程版
thread
socket1
thread
socket2
thread
socket3

  • • windows下一个线程就有1MB,若是连接不断增加,内存就会不足,可能产生OOM。

缺点:

  • • 内存占用高
  • • 线程上下文切换成本高
  • • 只适合连接数少的场景

线程多了CPU也要跟的上才行,若是你的电脑是16核,那么在同一时间跑的也只有16个线程,更多的线程只能等待,那么其他线程的状态信息都需要进行临时保存。(如执行函数、存储变量)

线程池版设计

早期的Tomcat就采用了这个线程池版设计

线程池版
thread
socket1
thread
socket2
socket3
socket4

缺点:

  • • 阻塞模式下,线程仅能处理一个 socket 连接
  • • 仅适合短连接场景:来了一个连接之后快速处理好业务直接就断开连接,好让线程池中的线程去处理其他事情。一般都是为http请求

selector 版设计

selector 的作用就是配合一个线程来管理多个 channel,获取这些 channel 上发生的事件,这些 channel 工作在非阻塞模式下,不会让线程吊死在一个 channel 上。适合连接数特别多,但流量低的场景(low traffic)

selector 版
thread
selector
channel
channel
channel

  • • 解释:在这里thread是服务员,channel是客人,而selector则是能够检测到所有客户需求的工具,对应channel的一举一动都在监视之中,一旦某个channel进行了请求,那么selector就会执行,紧接着selector就会去通知线程thread来对channel进行处理
  • • 问题(为什么适合流量低的原因,若是流量高呢?):若是某个连接不断的发送请求数据,那么就会造成一个问题就是线程会一直处理该channel的事件,那么其他事件都会一直在等待中。

调用 selector 的 select() 会阻塞直到 channel 发生了读写就绪事件,这些事件发生,select 方法就会返回这些事件交给 thread 来处理


二、ByteBuffer

ByteBuffer初应用

目标:使用channel以及bytebuffer来读取当前项目路径下的文件内容。

技术:channel+bytebuffer API、logback+lombok

<dependency>    <groupId>io.netty</groupId>    <artifactId>netty-all</artifactId>    <version>4.1.32.Final</version></dependency><dependency>    <groupId>org.projectlombok</groupId>    <artifactId>lombok</artifactId>    <version>1.18.20</version></dependency>

resources/loback.xml:用于指定日志的输出

<?xml version="1.0" encoding="UTF-8"?><configuration        xmlns="http://ch.qos.logback/xml/ns/logback"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback logback.xsd">    <!-- 输出控制,格式控制-->    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">        <encoder>            <pattern>%date{HH:mm:ss} [%-5level] [%thread] %logger{17} - %m%n </pattern>        </encoder>    </appender>    <!--<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">        &lt;!&ndash; 日志文件名称 &ndash;&gt;        <file>logFile.log</file>        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">            &lt;!&ndash; 每天产生一个新的日志文件 &ndash;&gt;            <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>            &lt;!&ndash; 保留 15 天的日志 &ndash;&gt;            <maxHistory>15</maxHistory>        </rollingPolicy>        <encoder>            <pattern>%date{HH:mm:ss} [%-5level] [%thread] %logger{17} - %m%n </pattern>        </encoder>    </appender>-->    <!-- 用来控制查看那个类的日志内容(对mybatis name 代表命名空间) -->    <logger name="com.changlu" level="DEBUG" additivity="false">        <appender-ref ref="STDOUT"/>    </logger>    <logger name="io.netty.handler.logging.LoggingHandler" level="DEBUG" additivity="false">        <appender-ref ref="STDOUT"/>    </logger>    <root level="ERROR">        <appender-ref ref="STDOUT"/>    </root></configuration>

项目目录/data.txt:文件内容

123456uiopqqrr

src/com/changlu/ByteBufferTest.java

import lombok.extern.slf4j.Slf4j;import java.io.FileInputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;/** * @ClassName ByteBufferTest * @Author ChangLu * @Date 2021/12/16 18:55 * @Description ByteBuffer基本使用 */@Slf4jpublic class ByteBufferTest {    public static void main(String[] args) {        //Channel:①输入输出流取得,如FileChannel。②RandomAccessFile        try (FileChannel channel = new FileInputStream("data.txt").getChannel()) {            //准备缓冲区:开辟一块10字节的堆内存空间            ByteBuffer byteBuffer = ByteBuffer.allocate(10);            while (true){                //从channel读取数据,向buffer写入                int len = channel.read(byteBuffer);                log.debug("已读取 {} 个字节", len);                if (len == -1){                    break;                }                byteBuffer.flip();//切换 buffer 读模式                // 在buffer中是否还有剩余的内容                while (byteBuffer.hasRemaining()) {                    byte data = byteBuffer.get();                    log.debug("当前读取的字节为:{}", (char)data);                }                byteBuffer.clear();// 切换 buffer 写模式            }        } catch (IOException e) {            e.printStackTrace();        }    }}

2.1、ByteBuffer正常使用流程(含源码)

正常流程

  1. 1. 向 buffer 写入数据,例如调用 channel.read(buffer)
  2. 2. 调用 flip() 切换至读模式
  3. 3. 从 buffer 读取数据,例如调用 buffer.get()
  4. 4. 调用 clear() 或 compact() 切换至写模式
  5. 5. 重复 1~4 步骤

读写模式源码

//切换为读模式public final Buffer flip() {    limit = position;//保存已写入的位置    position = 0;//待读取指针指向位置0开始    mark = -1;    return this;}//切换为写模式(未读的数据会被直接覆盖)public final Buffer clear() {    position = 0;//待写入指针指向位置从0开始    limit = capacity;//当前已有数量为原始容量数量(初始化)    mark = -1;    return this;}//切换为写模式(压缩未读完的空间至前,之后写入数据依次写入在未读的数据后)public ByteBuffer compact() {    System.arraycopy(hb, ix(position()), hb, ix(0), remaining());    position(remaining());    limit(capacity());    discardMark();    return this;}

2.2、ByteBuffer 结构

ByteBuffer 有以下重要属性

  • • capacity
  • • position
  • • limit

一开始(指初创建ByteBuffer对象后):

写模式下,position 是写入位置,limit 等于容量,下图表示写入了 4 个字节后的状态:

flip()发生后,position 切换为读取位置,limit 切换为读取限制:

读取 4 个字节后,状态:

clear()动作发生后,状态:进入写操作

compact()方法,是把未读完的部分向前压缩,然后切换至写模式


2.3、ByteBuffer方法演示

工具类(图形化显示bytebuffer)

使用方式

ByteBuffer buffer1 = ByteBuffer.allocate(16);//调用debugAll即可ByteBufferUtil.debugAll(buffer1);

工具类:其中使用Netty中的库

import io.netty.util.internal.StringUtil;import java.nio.ByteBuffer;import static io.netty.util.internal.MathUtil.isOutOfBounds;import static io.netty.util.internal.StringUtil.NEWLINE;public class ByteBufferUtil {    private static final char[] BYTE2CHAR = new char[256];    private static final char[] HEXDUMP_TABLE = new char[256 * 4];    private static final String[] HEXPADDING = new String[16];    private static final String[] HEXDUMP_ROWPREFIXES = new String[65536 >>> 4];    private static final String[] BYTE2HEX = new String[256];    private static final String[] BYTEPADDING = new String[16];    static {        final char[] DIGITS = "0123456789abcdef".toCharArray();        for (int i = 0; i < 256; i++) {            HEXDUMP_TABLE[i << 1] = DIGITS[i >>> 4 & 0x0F];            HEXDUMP_TABLE[(i << 1) + 1] = DIGITS[i & 0x0F];        }        int i;        // Generate the lookup table for hex dump paddings        for (i = 0; i < HEXPADDING.length; i++) {            int padding = HEXPADDING.length - i;            StringBuilder buf = new StringBuilder(padding * 3);            for (int j = 0; j < padding; j++) {                buf.append("   ");            }            HEXPADDING[i] = buf.toString();        }        // Generate the lookup table for the start-offset header in each row (up to 64KiB).        for (i = 0; i < HEXDUMP_ROWPREFIXES.length; i++) {            StringBuilder buf = new StringBuilder(12);            buf.append(NEWLINE);            buf.append(Long.toHexString(i << 4 & 0xFFFFFFFFL | 0x100000000L));            buf.setCharAt(buf.length() - 9, '|');            buf.append('|');            HEXDUMP_ROWPREFIXES[i] = buf.toString();        }        // Generate the lookup table for byte-to-hex-dump conversion        for (i = 0; i < BYTE2HEX.length; i++) {            BYTE2HEX[i] = ' ' + StringUtil.byteToHexStringPadded(i);        }        // Generate the lookup table for byte dump paddings        for (i = 0; i < BYTEPADDING.length; i++) {            int padding = BYTEPADDING.length - i;            StringBuilder buf = new StringBuilder(padding);            for (int j = 0; j < padding; j++) {                buf.append(' ');            }            BYTEPADDING[i] = buf.toString();        }        // Generate the lookup table for byte-to-char conversion        for (i = 0; i < BYTE2CHAR.length; i++) {            if (i <= 0x1f || i >= 0x7f) {                BYTE2CHAR[i] = '.';            } else {                BYTE2CHAR[i] = (char) i;            }        }    }    /**     * 打印所有内容     * @param buffer     */    public static void debugAll(ByteBuffer buffer) {        int oldlimit = buffer.limit();        buffer.limit(buffer.capacity());        StringBuilder origin = new StringBuilder(256);        appendPrettyHexDump(origin, buffer, 0, buffer.capacity());        System.out.println("+--------+-------------------- all ------------------------+----------------+");        System.out.printf("position: [%d], limit: [%d]\n", buffer.position(), oldlimit);        System.out.println(origin);        buffer.limit(oldlimit);    }    /**     * 打印可读取内容     * @param buffer     */    public static void debugRead(ByteBuffer buffer) {        StringBuilder builder = new StringBuilder(256);        appendPrettyHexDump(builder, buffer, buffer.position(), buffer.limit() - buffer.position());        System.out.println("+--------+-------------------- read -----------------------+----------------+");        System.out.printf("position: [%d], limit: [%d]\n", buffer.position(), buffer.limit());        System.out.println(builder);    }    public static void main(String[] args) {        ByteBuffer buffer = ByteBuffer.allocate(10);        buffer.put(new byte[]{97, 98, 99, 100});        debugAll(buffer);    }    private static void appendPrettyHexDump(StringBuilder dump, ByteBuffer buf, int offset, int length) {        if (isOutOfBounds(offset, length, buf.capacity())) {            throw new IndexOutOfBoundsException(                    "expected: " + "0 <= offset(" + offset + ") <= offset + length(" + length                            + ") <= " + "buf.capacity(" + buf.capacity() + ')');        }        if (length == 0) {            return;        }        dump.append(                "         +-------------------------------------------------+" +                        NEWLINE + "         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |" +                        NEWLINE + "+--------+-------------------------------------------------+----------------+");        final int startIndex = offset;        final int fullRows = length >>> 4;        final int remainder = length & 0xF;        // Dump the rows which have 16 bytes.        for (int row = 0; row < fullRows; row++) {            int rowStartIndex = (row << 4) + startIndex;            // Per-row prefix.            appendHexDumpRowPrefix(dump, row, rowStartIndex);            // Hex dump            int rowEndIndex = rowStartIndex + 16;            for (int j = rowStartIndex; j < rowEndIndex; j++) {                dump.append(BYTE2HEX[getUnsignedByte(buf, j)]);            }            dump.append(" |");            // ASCII dump            for (int j = rowStartIndex; j < rowEndIndex; j++) {                dump.append(BYTE2CHAR[getUnsignedByte(buf, j)]);            }            dump.append('|');        }        // Dump the last row which has less than 16 bytes.        if (remainder != 0) {            int rowStartIndex = (fullRows << 4) + startIndex;            appendHexDumpRowPrefix(dump, fullRows, rowStartIndex);            // Hex dump            int rowEndIndex = rowStartIndex + remainder;            for (int j = rowStartIndex; j < rowEndIndex; j++) {                dump.append(BYTE2HEX[getUnsignedByte(buf, j)]);            }            dump.append(HEXPADDING[remainder]);            dump.append(" |");            // Ascii dump            for (int j = rowStartIndex; j < rowEndIndex; j++) {                dump.append(BYTE2CHAR[getUnsignedByte(buf, j)]);            }            dump.append(BYTEPADDING[remainder]);            dump.append('|');        }        dump.append(NEWLINE +                "+--------+-------------------------------------------------+----------------+");    }    private static void appendHexDumpRowPrefix(StringBuilder dump, int row, int rowStartIndex) {        if (row < HEXDUMP_ROWPREFIXES.length) {            dump.append(HEXDUMP_ROWPREFIXES[row]);        } else {            dump.append(NEWLINE);            dump.append(Long.toHexString(rowStartIndex & 0xFFFFFFFFL | 0x100000000L));            dump.setCharAt(dump.length() - 9, '|');            dump.append('|');        }    }    public static short getUnsignedByte(ByteBuffer buffer, int index) {        return (short) (buffer.get(index) & 0xFF);    }}

2.3.1、allocate()、allocateDirect()(分配空间)

各个空间开辟问题

  • • 开辟在堆内存的问题:会受到GC垃圾回收的影响,一旦进行GC回收,那么堆内存就会被移动,势必就会造成性能影响。
  • • 直接内存问题:不会受到GC影响,并非使用的是JVM堆中的内存,使用的是操作系统的。分配比较慢,因为其需要调用操作系统接口,若是使用不当就会造成内存泄漏,需要进行合理的释放。
/** * @ClassName TestByteBufferAllocate * @Author ChangLu * @Date 2021/12/16 20:41 * @Description ByteBufferAllocate:开辟空间 */public class TestByteBufferAllocate {    public static void main(String[] args) {        //开启堆内存        System.out.println(ByteBuffer.allocate(10).getClass());        //开辟直接内存        System.out.println(ByteBuffer.allocateDirect(10).getClass());        /*        class java.nio.HeapByteBuffer    - java 堆内存,读写效率较低,受到 GC 的影响        class java.nio.DirectByteBuffer  - 直接内存,读写效率高(少一次拷贝),不会受 GC 影响,分配的效率低         */    }}

2.3.2、put()、flip()、clear()、compact()、get()

import java.nio.ByteBuffer;import static com.changlu.utils.ByteBufferUtil.debugAll;public class TestByteBufferReadWrite {    /**     * put():写入字节,position+n     * flip():进入读模式,limit=position,position=0,     * get():读取position开始的字节,position+n     * clear():进入写模式,position=0,limit=capacity     * compact():压缩内容,将未读取的字节向前移,...,position=limit,limit=capacity     *     * @param args     */    public static void main(String[] args) {        ByteBuffer buffer = ByteBuffer.allocate(16);        //写入一个字节        buffer.put((byte)0x61);        debugAll(buffer);        //进入读模式        buffer.flip();        debugAll(buffer);        //读取一个字节        System.out.println("读取字符:" + (char)buffer.get());        debugAll(buffer);        //测试compact():切换写模式,写入三个字节,切换读模式,读取一个字节,进行压缩(保留未读字节向前移动,position指向未读字节之后)        buffer.clear();        buffer.put(new byte[]{0x62,0x63,0x64});        buffer.flip();        System.out.println("读取字符:" + (char)buffer.get());        buffer.compact();        debugAll(buffer);    }}

效果:

+--------+-------------------- all ------------------------+----------------+position: [1], limit: [16]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |a...............|+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [0], limit: [1]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |a...............|+--------+-------------------------------------------------+----------------+读取字符:a+--------+-------------------- all ------------------------+----------------+position: [1], limit: [1]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |a...............|+--------+-------------------------------------------------+----------------+读取字符:b+--------+-------------------- all ------------------------+----------------+position: [2], limit: [16]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 63 64 64 00 00 00 00 00 00 00 00 00 00 00 00 00 |cdd.............|+--------+-------------------------------------------------+----------------+

2.3.3、get()、put()(读取与写入数据)

读取

同样有两种办法

  • • 调用 channel 的 write 方法
  • • 调用 buffer 自己的 get 方法
int writeBytes = channel.write(buf);//从bytebuffer中读取写入到channel中

byte b = buf.get();//直接从bytebuffer中取到1个字节

get 方法会让 position 读指针向后走,如果想重复读取数据

  • • 可以调用 rewind 方法将 position 重新置为 0
  • • 或者调用 get(int i) 方法获取索引 i 的内容,它不会移动读指针position

写入

有两种办法

  • • 调用 channel 的 read 方法
  • • 调用 buffer 自己的 put 方法
int readBytes = channel.read(buf);//从channel中读取到数据写入到bytebuffer里

buf.put((byte)127);//直接向bytebuffer写入一个字节

2.3.4、rewind()(重置position=0)

目的:想要重新重头读取一遍数据。

import java.nio.ByteBuffer;import static com.changlu.utils.ByteBufferUtil.debugAll;/** * @ClassName TestByteBufferRewind * @Author ChangLu * @Date 2021/12/16 20:51 * @Description 测试ByteBuffer的Rewind()方法 */public class TestByteBufferRewind {    public static void main(String[] args) {        ByteBuffer buffer = ByteBuffer.allocate(10);        buffer.put(new byte[]{0x61,0x62,0x63});        buffer.flip();        debugAll(buffer);        for (int i = 0; i < 3; i++) {            if (i == 0)                System.out.print("读取三个字节:");            System.out.print((char)buffer.get() + " ");        }        System.out.println();        debugAll(buffer);        //rewind()(重复读取一遍):position = 0        buffer.rewind();        System.out.println("执行rewind后:");        debugAll(buffer);        System.out.println("重新读取一遍:");        for (int i = 0; i < 3; i++) {            if (i == 0)                System.out.print("读取三个字节:");            System.out.print((char)buffer.get() + " ");        }    }}

效果:

+--------+-------------------- all ------------------------+----------------+position: [0], limit: [3]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 61 62 63 00 00 00 00 00 00 00                   |abc.......      |+--------+-------------------------------------------------+----------------+读取三个字节:a b c +--------+-------------------- all ------------------------+----------------+position: [3], limit: [3]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 61 62 63 00 00 00 00 00 00 00                   |abc.......      |+--------+-------------------------------------------------+----------------+执行rewind后:+--------+-------------------- all ------------------------+----------------+position: [0], limit: [3]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 61 62 63 00 00 00 00 00 00 00                   |abc.......      |+--------+-------------------------------------------------+----------------+重新读取一遍:读取三个字节:a b c 

2.3.5、remark()与reset()(打标记与重置指针位置)

目的:若是我们想要重复读某个指定位置开始的内容,可使用标记与恢复标记的方式进行。

import java.nio.ByteBuffer;import static com.changlu.utils.ByteBufferUtil.debugAll;/** * @ClassName TestByteBufferMark_Reset * @Author ChangLu * @Date 2021/12/16 21:03 * @Description 测试ByteBuffer的方法:mark()、reset() */public class TestByteBufferMark_Reset {    public static void main(String[] args) {        ByteBuffer buffer = ByteBuffer.allocate(16);        buffer.put(new byte[]{0x62,0x63,0x64});        buffer.flip();        System.out.println((char)buffer.get());        buffer.mark();//mark进行标记该位置:mark=position        System.out.println((char)buffer.get());        System.out.println((char)buffer.get());        debugAll(buffer);        buffer.reset();//恢复到原标记位置:position=mark        debugAll(buffer);        //重复读两次        System.out.println("恢复标记后,重新读取:");        System.out.println((char)buffer.get());        System.out.println((char)buffer.get());    }}

效果展示:

bcd+--------+-------------------- all ------------------------+----------------+position: [3], limit: [3]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 62 63 64 00 00 00 00 00 00 00 00 00 00 00 00 00 |bcd.............|+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [1], limit: [3]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 62 63 64 00 00 00 00 00 00 00 00 00 00 00 00 00 |bcd.............|+--------+-------------------------------------------------+----------------+恢复标记后,重新读取:cd

2.3.6、String与ByteBuffer互转

其中StandardCharsets.UTF_8.encode进行编码得到的ByteBuffer对象会进行转换读模式(position=0)!

import java.nio.ByteBuffer;import java.nio.charset.StandardCharsets;import static com.changlu.utils.ByteBufferUtil.debugAll;/** * @ClassName TestStringTOByteBuffer * @Author ChangLu * @Date 2021/12/16 21:13 * @Description String与ByteBuffer互转 */public class TestStringTOByteBuffer {    public static void main(String[] args) {        ByteBuffer buffer = ByteBuffer.allocate(16);        //字符串转为bytebuffer        //方式一:put()时直接字符串转为byte[]写入        buffer.put("changlu".getBytes());        debugAll(buffer);        //方式二:StandardCharsets.UTF_8进行编码创建对象。        //内部过程:按需创建空间,依次填入内容后,执行写模式        ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("changlu");        debugAll(buffer2);        //方式三:ByteBuffer.wrap()        ByteBuffer buffer3 = ByteBuffer.wrap("changlu".getBytes());        debugAll(buffer3);        //ByteBuffer转字符串        //方式一:StandardCharsets.UTF_8.decode,原理就是从position位置开始读取已有的内容        System.out.println(StandardCharsets.UTF_8.decode(buffer2).toString());        //注意:对于上面方式一put()进入由于没有转换为读状态所以需要先进行转换状态,也就是要position=0        buffer.flip();        System.out.println(StandardCharsets.UTF_8.decode(buffer).toString());    }}

效果:

+--------+-------------------- all ------------------------+----------------+position: [7], limit: [16]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 63 68 61 6e 67 6c 75 00 00 00 00 00 00 00 00 00 |changlu.........|+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [0], limit: [7]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 63 68 61 6e 67 6c 75                            |changlu         |+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [0], limit: [7]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 63 68 61 6e 67 6c 75                            |changlu         |+--------+-------------------------------------------------+----------------+changluchanglu

快速总结

ByteBuffer方法

方法
效果
put()
postion += n
get()
position += n
get(index)
直接取到内容,position不动
flip()
读状态:limit = position,position=0
clear()
写状态:position=0
compact()
写状态:未读数据压缩,position=limit+1,limit=capacity
rewind()
重置:position=0
remark()
mark=position
reset()
position=mark

String与ByteBuffer互转方法

String => ByteBuffer

方法
效果
buffer.put("changlu".getBytes())
写入方式,position += n
StandardCharsets.UTF_8.encode("changlu")
按需创建空间,position = 0
ByteBuffer.wrap("changlu".getBytes())
按需创建空间,position = 0

ByteBuffer => String

方法
效果
StandardCharsets.UTF_8.decode(buffer2).toString()
需要当前的buffer为读模式,也就是position应当为0

2.4、ByteBuffer分散读写

分散集中读、写好处:能够减少ByteBuffer之间的拷贝,减少数据的复制次数,变相的提高了效率。下面是读写的示例:

Scattering Reads(分散读)

import java.io.IOException;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import static com.changlu.utils.ByteBufferUtil.debugAll;/** * @ClassName ScatteringReads * @Author ChangLu * @Date 2021/12/17 17:32 * @Description 分散读取文件内容至ByteBuffer */public class ScatteringReads {    public static void main(String[] args) {        ByteBuffer b1 = ByteBuffer.allocate(7);        ByteBuffer b2 = ByteBuffer.allocate(5);        ByteBuffer b3 = ByteBuffer.allocate(3);        try (FileChannel channel = new RandomAccessFile("data1.txt", "r").getChannel()) {            //直接读取内容到各个ByteBuffer对象中            channel.read(new ByteBuffer[]{b1,b2,b3});            debugAll(b1);            debugAll(b2);            debugAll(b3);        } catch (IOException e) {        }    }}
//data1.txt文件内容changluliner123

效果:

+--------+-------------------- all ------------------------+----------------+position: [7], limit: [7]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 63 68 61 6e 67 6c 75                            |changlu         |+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [5], limit: [5]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 6c 69 6e 65 72                                  |liner           |+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [3], limit: [3]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 31 32 33                                        |123             |+--------+-------------------------------------------------+----------------+

分散写

import io.netty.util.CharsetUtil;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;/** * @ClassName GatheringWrites * @Author ChangLu * @Date 2021/12/17 17:37 * @Description 分散写:将多个ByteBuffer中内容依次写入到一个文件中 */public class GatheringWrites {    public static void main(String[] args) {        ByteBuffer b1 = CharsetUtil.UTF_8.encode("changlu");        ByteBuffer b2 = CharsetUtil.UTF_8.encode("hennuli");        try (FileChannel channel = new RandomAccessFile("data3.txt", "rw").getChannel()) {            //将多个ByteBuffer中存储的内容依次写入到文件中            channel.write(new ByteBuffer[]{b1,b2});        } catch (IOException e) {        }    }}

效果:

//data3.txtchangluhennuli

2.5、网络黏包、半包案例(重要)

案例分析

网络上有多条数据发送给服务端,数据之间使用 \n 进行分隔但由于某种原因这些数据在接收时,被进行了重新组合,例如原始数据有3条为

  • • Hello,world\n
  • • I'm zhangsan\n
  • • How are you?\n

变成了下面的两个 byteBuffer (黏包,半包)

  • • Hello,world\nI'm zhangsan\nHo
  • • w are you?\n
若是两个消息和在一起就是粘包,若是消息被截断到下一条消息里就是半包!黏包出现原因:由于效率原因(多个打包比一个一个打包发出去肯定高),其也就会出现黏包的原因。半包出现原因:由于服务器的缓冲大小限制决定的,例如ByteBuffer不可能无限大,那么一次发送过来的内容ByteBuffer只能接收其所能接收的部分,那么剩余的一部分只能下一次接收,此时就产生半包现象。

现在要求你编写程序,将错乱的数据恢复成原始的按 \n 分隔的数据

代码编写

现在我们直接向Buffer中输入指定黏包、半包情况,接着对ByteBuffer中的内容进行解析读取:

import java.nio.ByteBuffer;import static com.changlu.utils.ByteBufferUtil.debugAll;/** * @ClassName ByteBufferExam * @Author ChangLu * @Date 2021/12/17 17:58 * @Description 黏包、半包解析(底层实现) */public class ByteBufferExam {    public static void main(String[] args) {        ByteBuffer buffer = ByteBuffer.allocate(256);        //黏包情况:        buffer.put("I am changlu!\nhello,xiaoli\nha".getBytes());        handle(buffer);        //半包情况:        buffer.put(",are you ok?\n".getBytes());        handle(buffer);    }    /**     * 处理黏包、半包情况:每次能够将\n结尾的内容读取到一个ByteBuffer,并测该ByteBuffer对象     * @param buffer     */    private static void handle(ByteBuffer buffer) {        buffer.flip();//切换到读状态        for (int i = 0; i < buffer.limit(); i++) {            if (buffer.get(i) == '\n') {                int readLen = i - buffer.position() + 1;                ByteBuffer temp = ByteBuffer.allocate(20);                for (int j = 0; j < readLen; j++) {                    temp.put(buffer.get());                }                debugAll(temp);            }        }        buffer.compact();//切换写状态(压缩):保留未读取的内容    }}

效果展示:可以看到三条信息都被单独读取并处理!

+--------+-------------------- all ------------------------+----------------+position: [14], limit: [20]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 49 20 61 6d 20 63 68 61 6e 67 6c 75 21 0a 00 00 |I am changlu!...||00000010| 00 00 00 00                                     |....            |+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [13], limit: [20]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 68 65 6c 6c 6f 2c 78 69 61 6f 6c 69 0a 00 00 00 |hello,xiaoli....||00000010| 00 00 00 00                                     |....            |+--------+-------------------------------------------------+----------------++--------+-------------------- all ------------------------+----------------+position: [15], limit: [20]         +-------------------------------------------------+         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |+--------+-------------------------------------------------+----------------+|00000000| 68 61 2c 61 72 65 20 79 6f 75 20 6f 6b 3f 0a 00 |ha,are you ok?..||00000010| 00 00 00 00                                     |....            |+--------+-------------------------------------------------+----------------+

三、文件编程

3.1、FileChannel

FileChannel:只能工作在阻塞模式下。

获取

不能直接打开 FileChannel,必须通过 FileInputStream、FileOutputStream 或者 RandomAccessFile 来获取 FileChannel,它们都有 getChannel 方法

  • • 通过 FileInputStream 获取的 channel 只能读
  • • 通过 FileOutputStream 获取的 channel 只能写
  • • 通过 RandomAccessFile 是否能读写根据构造 RandomAccessFile 时的读写模式决定

读取

会从 channel 读取数据填充 ByteBuffer,返回值表示读到了多少字节,-1 表示到达了文件的末尾

int readBytes = channel.read(buffer);

写入

写入的正确姿势如下, SocketChannel

ByteBuffer buffer = ...;buffer.put(...); // 存入数据buffer.flip();   // 切换读模式while(buffer.hasRemaining()) {    channel.write(buffer);}

在 while 中调用 channel.write 是因为 write 方法并不能保证一次将 buffer 中的内容全部写入 channel


关闭

channel 必须关闭,不过调用了 FileInputStream、FileOutputStream 或者 RandomAccessFile 的 close 方法会间接地调用 channel 的 close 方法


位置

获取当前位置

long pos = channel.position();

设置当前位置

long newPos = ...;channel.position(newPos);

设置当前位置时,如果设置为文件的末尾

  • • 这时读取会返回 -1
  • • 这时写入,会追加内容,但要注意如果 position 超过了文件末尾,再写入时在新内容和原末尾之间会有空洞(00)

大小

使用 size 方法获取文件的大小


强制写入

操作系统出于性能的考虑,会将数据缓存,不是立刻写入磁盘。可以调用 force(true)  方法将文件内容和元数据(文件的权限等信息)立刻写入磁盘

注意:只有当channel关闭的时候才会同步到磁盘上去,也可以通过编程式直接写入,不过对性能会有影响!


3.2、两个Channel传输数据(transferTo())

目的:进行文件的复制拷贝。

底层:使用操作系统的零拷贝。

方式:使用Channel的transferTo()方法,需要注意的是每次方法调用只能拷贝2GB容量,所以对于大于2G大小的需要多次重复调用。

代码说明:两个demo复制拷贝方法,一个是>2G的,另一个是<=2G。

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;/** * @ClassName TestTransferTo * @Author ChangLu * @Date 2021/12/17 19:26 * @Description 测试FileChannel的transferTo方法:底层为零拷贝 */public class TestTransferTo {    public static void main(String[] args) {        String FILE_FROM = "C:\\Users\\93997\\Desktop\\新建文件夹\\这个杀手不太冷1994.mp4";        String FILE_TO = "C:\\Users\\93997\\Desktop\\新建文件夹\\新建文件夹\\这个杀手不太冷1994.mp4";        final long begin = System.currentTimeMillis();        System.out.println("复制中...");        //传输<=2G//        transferTo_2G(FILE_FROM, FILE_TO);        //>2G        transferTo_t2G(FILE_FROM, FILE_TO);        System.out.println("耗时:" + (System.currentTimeMillis() - begin) / 1000 + "s");    }    /**     * 传输超过<=2G容量大小,默认transferTo传输一次上限为2G容量     */    public static void transferTo_2G(String FILE_FROM, String FILE_TO){        try (            FileChannel from = new FileInputStream(FILE_FROM).getChannel();            FileChannel to = new FileOutputStream(FILE_TO).getChannel();        ) {            final long size = from.size();            //注意:传输默认上限最大为2G            from.transferTo(0, size, to);        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 传输超过2G容量大小     */    public static void transferTo_t2G(String FILE_FROM, String FILE_TO){        try (                FileChannel from = new FileInputStream(FILE_FROM).getChannel();                FileChannel to = new FileOutputStream(FILE_TO).getChannel();        ) {            //通过传输容量与原始容量比对来解决一次传输>2G文件的情况            long leftSize = from.size();            while (true) {                leftSize -= from.transferTo(from.size() - leftSize, leftSize, to);                System.out.println("已拷贝:" + (from.size() - leftSize)/1024.0/1024.0 + "MB,剩余:" + leftSize/1024.0/1024.0 + "MB");                if (leftSize < 2 * 1024 * 1024){                    break;                }            }        } catch (IOException e) {            e.printStackTrace();        }    }}

3.3、Path类、Paths工具类

jdk7 引入了 Path 和 Paths 类

  • • Path 用来表示文件路径
  • • Paths 是工具类,用来获取 Path 实例

基本的Paths.get()路径设置:

//基本的Path.get()格式public static void test01(){    // 相对路径 使用 user.dir 环境变量来定位 1.txt    Path path1 = Paths.get("1.txt");    System.out.println(path1);//1.txt    // 绝对路径 代表了  d:\1.txt    Path path2 = Paths.get("E:\\资源\\电影资源\\1.txt");    System.out.println(path2);//E:\资源\电影资源\1.txt    // 绝对路径 同样代表了  d:\1.txt    Path path3 = Paths.get("d:/1.txt");    System.out.println(path3);    // 代表了  d:\data\projects    Path path4 = Paths.get("d:\\data", "changlu.txt");    System.out.println(path4);}

较特殊的路径格式:可转义..

//调用normalize()可对路径进行转义:..表示上级public static void test02(){    Path path = Paths.get("d:\\data\\projects\\a\\..\\b");    System.out.println(path);    System.out.println(path.normalize());}

3.4、Files工具类(自带传输方法copy)

3.4.1、基本API使用

检查文件是否存在

Path path = Paths.get("helloword/data.txt");System.out.println(Files.exists(path));

创建目录

创建一级目录:

Path path = Paths.get("helloword/d1");Files.createDirectory(path);
  • • 如果目录已存在,会抛异常 FileAlreadyExistsException
  • • 不能一次创建多级目录,否则会抛异常 NoSuchFileException

创建多级目录:

Path path = Paths.get("helloword/d1/d2");Files.createDirectories(path);

拷贝文件

Path source = Paths.get("helloword/data.txt");Path target = Paths.get("helloword/target.txt");Files.copy(source, target);//底层使用的操作系统的拷贝,性能也很好!与transferTo相似。
  • • 如果文件已存在,会抛异常 FileAlreadyExistsException

如果希望用 source 覆盖掉 target,需要用 StandardCopyOption 来控制

Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);

移动文件

移动文件

Path source = Paths.get("helloword/data.txt");Path target = Paths.get("helloword/data.txt");Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
  • • StandardCopyOption.ATOMIC_MOVE 保证文件移动的原子性

删除

删除文件:

Path target = Paths.get("helloword/target.txt");Files.delete(target);
  • • 如果文件不存在,会抛异常 NoSuchFileException

删除目录:

Path target = Paths.get("helloword/d1");Files.delete(target);
  • • 如果目录还有内容,会抛异常 DirectoryNotEmptyException

3.4.2、walk、walkFileTree(递归遍历文件目录工具方法)

方法介绍

//递归遍历文件目录,返回的是一个文件Path Stream流public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException {    return walk(start, Integer.MAX_VALUE, options);}
//递归遍历文件目录,访问者模式,重写接口方法public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor)    throws IOException{    return walkFileTree(start,                        EnumSet.noneOf(FileVisitOption.class),                        Integer.MAX_VALUE,                        visitor);}//访问接口public interface FileVisitor<T> {    //访问文件目录前    FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)        throws IOException;    //访问文件时    FileVisitResult visitFile(T file, BasicFileAttributes attrs)        throws IOException;    //访问文件失败    FileVisitResult visitFileFailed(T file, IOException exc)        throws IOException;    //访问文件目录后    FileVisitResult postVisitDirectory(T dir, IOException exc)        throws IOException;}

使用场景:

  • • walk():返回的是stream流,流中包含所有的文件Path对象,可以进行过滤、统计、复制一系列操作。
  • • walkFileTree():在递归过程中,可以通过接口重写的方式在不同的文件或目录访问时刻进行一些操作。

功能1:遍历目录文件

/**  * 单独功能点1:遍历目录文件,打印出文件夹数量、文件数。  *      说明:这里将root文件也算入文件夹的数量中,所以内部文件夹数量应该-1  */private static void testViewAllFiles() throws IOException {    final Path path = Paths.get("C:\\Program Files\\Java\\jdk1.8.0_201");    //由于匿名实现类中重写方法不能直接对局部变量进行改变值操作所以使用AtomicInteger    AtomicInteger dirCount = new AtomicInteger();    AtomicInteger fileCount = new AtomicInteger();    //递归遍历文件夹    Files.walkFileTree(path,new SimpleFileVisitor<Path>() {        //进入目录前        @Override        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {            System.out.println("进入文件目录=》" + dir);            dirCount.getAndIncrement();            return super.preVisitDirectory(dir, attrs);        }        //访问当前文件        @Override        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {            System.out.println("           =》" + file);            fileCount.getAndIncrement();            return super.visitFile(file, attrs);        }    });    System.out.println("文件夹数量:" + dirCount);    System.out.println("文件数量:" + fileCount);}

功能2:统计JDK8目录中jar包的个数

/**  * 单独功能点2:统计JDK8目录中jar包的个数  */private static void testJarFileCount() throws IOException {    final Path path = Paths.get("C:\\Program Files\\Java\\jdk1.8.0_201");    AtomicInteger jarCount = new AtomicInteger();    //递归遍历文件夹    Files.walkFileTree(path,new SimpleFileVisitor<Path>() {        //访问当前文件        @Override        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {            //注意:file.getFileName()返回值是Path            if (file.toFile().getName().endsWith(".jar")){                System.out.println(file.getFileName());                jarCount.getAndIncrement();            }            return super.visitFile(file, attrs);        }    });    System.out.println("jar包数量:" + jarCount);}

功能3:删除多级目录(不走回收站,谨慎删除)

/**  * 单独功能点3:删除多级目录(不走回收站,谨慎删除)  */private static void testFileMultDirs() throws IOException {    final Path path = Paths.get("C:\\Users\\93997\\Desktop\\新建文件夹\\工作室网站");    //注意点:删除多级目录时,首先需要先将某个文件夹中的文件内容删除,之后才可以删除文件目录    Files.walkFileTree(path,new SimpleFileVisitor<Path>() {        //1、先删除文件        @Override        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {            Files.delete(file);            return super.visitFile(file, attrs);        }        //2、后删除目录(后置文件访问器)        @Override        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {            Files.delete(dir);            return super.postVisitDirectory(dir, exc);        }    });}

####功能4、拷贝文件夹到指定目录

/**  * 单独功能点4、拷贝文件夹到指定目录  */private static void testCopyMultDir() throws IOException {    String COPY_ORIGIN = "C:\\Users\\93997\\Desktop\\工作室网站";    //复制到的文件目录为:指定路径\\工作室网站1    String COPY_TARGET = "C:\\Users\\93997\\Desktop\\新建文件夹\\工作室网站1";    //递归返回所有的文件Path集合stream流    Files.walk(Paths.get(COPY_ORIGIN)).forEach(path->{        final String copyFileName = path.toFile().getAbsolutePath().replace(COPY_ORIGIN, COPY_TARGET);        System.out.println(copyFileName);        //若是文件先创建文件        try {            if (Files.isDirectory(path)){                Files.createDirectory(Paths.get(copyFileName));            } else {                Files.copy(path, Paths.get(copyFileName));            }        } catch (IOException e) {            e.printStackTrace();        }    });}

END

扫码二维码

获取更多精彩

感谢您的关注

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-05-06 19:24:20 HTTP/2.0 GET : https://67808.cn/a/486527.html
  2. 运行时间 : 0.182436s [ 吞吐率:5.48req/s ] 内存消耗:4,577.93kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=712c3e646c98ab0405edeb50a8dd3de1
  1. /yingpanguazai/ssd/ssd1/www/no.67808.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/no.67808.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/no.67808.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/no.67808.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/no.67808.cn/runtime/temp/6df755f970a38e704c5414acbc6e8bcd.php ( 12.06 KB )
  140. /yingpanguazai/ssd/ssd1/www/no.67808.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000499s ] mysql:host=127.0.0.1;port=3306;dbname=no_67808;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000603s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000304s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.001598s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000643s ]
  6. SELECT * FROM `set` [ RunTime:0.001513s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000791s ]
  8. SELECT * FROM `article` WHERE `id` = 486527 LIMIT 1 [ RunTime:0.002852s ]
  9. UPDATE `article` SET `lasttime` = 1778066660 WHERE `id` = 486527 [ RunTime:0.012114s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 65 LIMIT 1 [ RunTime:0.000298s ]
  11. SELECT * FROM `article` WHERE `id` < 486527 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.003174s ]
  12. SELECT * FROM `article` WHERE `id` > 486527 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.031018s ]
  13. SELECT * FROM `article` WHERE `id` < 486527 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.011213s ]
  14. SELECT * FROM `article` WHERE `id` < 486527 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.030389s ]
  15. SELECT * FROM `article` WHERE `id` < 486527 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.011619s ]
0.183969s