Bytes int java_Java Bytes.readInt方法代码示例

本文展示了如何在Java中使用Bytes.readInt方法,包括读取整数、处理异常和字符编码转换的详细步骤。通过示例代码,解释了在读取过程中如何确保数据完整性和处理潜在的溢出问题。
摘要由CSDN通过智能技术生成

import net.openhft.chronicle.bytes.Bytes; //导入方法依赖的package包/类

@NotNull

@Override

public CharSequence read(Bytes in, @Nullable CharSequence using) {

long csLengthAsLong = in.readStopBit();

if (csLengthAsLong > Integer.MAX_VALUE) {

throw new IORuntimeException("cs len shouldn't be more than " + Integer.MAX_VALUE +

", " + csLengthAsLong + " read");

}

int csLength = (int) csLengthAsLong;

StringBuilder sb;

if (using instanceof StringBuilder) {

sb = (StringBuilder) using;

sb.setLength(0);

sb.ensureCapacity(csLength);

} else {

sb = new StringBuilder(csLength);

}

int remainingBytes = in.readInt();

charsetDecoder.reset();

inputBuffer.clear();

outputBuffer.clear();

boolean endOfInput = false;

// this loop inspired by the CharsetDecoder.decode(ByteBuffer) implementation

while (true) {

if (!endOfInput) {

int inputChunkSize = Math.min(inputBuffer.remaining(), remainingBytes);

inputBuffer.limit(inputBuffer.position() + inputChunkSize);

in.read(inputBuffer);

inputBuffer.flip();

remainingBytes -= inputChunkSize;

endOfInput = remainingBytes == 0;

}

CoderResult cr = inputBuffer.hasRemaining() ?

charsetDecoder.decode(inputBuffer, outputBuffer, endOfInput) :

CoderResult.UNDERFLOW;

if (cr.isUnderflow() && endOfInput)

cr = charsetDecoder.flush(outputBuffer);

if (cr.isUnderflow()) {

if (endOfInput) {

break;

} else {

inputBuffer.compact();

continue;

}

}

if (cr.isOverflow()) {

outputBuffer.flip();

sb.append(outputBuffer);

outputBuffer.clear();

continue;

}

try {

cr.throwException();

} catch (CharacterCodingException e) {

throw new IORuntimeException(e);

}

}

outputBuffer.flip();

sb.append(outputBuffer);

return sb;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值