org.artifact.core.IByteBuffTest Maven / Gradle / Ivy
package org.artifact.core;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.artifact.core.context.bytebuf.ArrayByteBuffFactory;
import org.artifact.core.context.bytebuf.IByteBuff;
import org.artifact.core.context.bytebuf.IByteBuffFactory;
import org.artifact.core.context.bytebuf.NettyByteBuffFactory;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.ArrayUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class IByteBuffTest {
public static void main(String[] args) {
ByteBuf byteBuff = Unpooled.directBuffer();
ByteBuffer byteBuffer = byteBuff.nioBuffer();
System.out.println(byteBuffer.array());
}
private static void test1() {
Date date = new Date();
// IByteBuffFactory factory = new NettyByteBuffFactory();
IByteBuffFactory factory = new ArrayByteBuffFactory();
TimeInterval timer = DateUtil.timer();
for (int i = 0; i < 1000; i++) {
// System.out.println("--------------------------------");
IByteBuff byteBuff = factory.allocate();
byteBuff.writeByte((byte) -50);
byteBuff.writeByte((byte) -1);
byteBuff.writeByte((byte) 0);
byteBuff.writeByte((byte) 15);
byteBuff.writeShort((short) -24);
byteBuff.writeShort((short) 456);
byteBuff.writeInt(456489798);
byteBuff.writeInt(12);
byteBuff.writeLong(456456456489798L);
byteBuff.writeLong(-12);
byteBuff.writeFloat(0.45f);
byteBuff.writeFloat(4564.04f);
byteBuff.writeDouble(7878.45d);
byteBuff.writeDouble(-4564.04d);
byteBuff.writeDate(date);
byteBuff.writeString("fdsafdsafcvxz我操~@!!~#");
List list = new ArrayList<>();
list.add("cvz");
list.add(132);
list.add(132.498f);
list.add(32.498d);
list.add(date);
byteBuff.writeCollection(list);
Map map = new HashMap<>();
map.put("1", 1);
map.put(456.0f, "float");
map.put(0.01d, "double");
byteBuff.writeMap(map);
print(byteBuff.readByte());
print(byteBuff.readByte());
print(byteBuff.readByte());
print(byteBuff.readByte());
print(byteBuff.readShort());
print(byteBuff.readShort());
print(byteBuff.readInt());
print(byteBuff.readInt());
print(byteBuff.readLong());
print(byteBuff.readLong());
print(byteBuff.readFloat());
print(byteBuff.readFloat());
print(byteBuff.readDouble());
print(byteBuff.readDouble());
print(byteBuff.readDate());
print(byteBuff.readString());
print(byteBuff.readCollection());
print(byteBuff.readMap());
}
System.out.println(timer.interval());
}
public static void print(Object obj) {
// System.out.println(obj);
}
}