java.io.UTFDataFormatException: encoded string too long: 270976 bytes

DataOutputStream extend FilterOutputStream implements DataOutput

function: 数据输出流是应用程序以便携式方式将java基本数据类型(包含String类型)写入到输出流,然后应用程序使用数据输入流来读取数据。

需求: 上传日志报文

日志报文格式(字节):

  • 日志标识[1]
  • 保留字段[1]
  • 日志类型编码[1]
  • 日志子类型编码[1]
  • 数据包长度[4]
  • 日志内容[n]

根据需求使用输出流的包装类DataOutputStream来进行写文件,主要是很方便,都封装好了。

    /**
     *
     * @param fileName 文件名
     * @return
     */
    public File toFile(String fileName) throws IOException {
        File file = new File(fileName);
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
        dos.writeByte(this.logIdf);
        dos.writeByte(this.reserve);
        dos.writeByte(this.logType);
        dos.writeByte(this.logSubType);
        dos.writeInt(getLength());
        if (this.content == null) {
            this.content = new JSONObject();
        }
        dos.writeUTF(this.content.toJSONString());
        dos.flush();
        dos.close();
        return file;
    }

在我本地的测试机上进行测试,发现没有什么问题。(content内容主要记录ssh运维过程中传输的文件名称,操作的指令等等,本地没有搞那么多,测试传了上千个文件),提交测试后就出问题了。错误日志java.io.UTFDataFormatException encoded string too long: 270976 bytes"经过排查日志发现问题就出在了dos.writeUTF(this.content.toJSONString());这一行代码。

源码:

 static int writeUTF(String str, DataOutput out) throws IOException {
        int strlen = str.length();
        int utflen = 0;
        int c, count = 0;

        /* use charAt instead of copying String to char array */
        for (int i = 0; i < strlen; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
        }

        if (utflen > 65535)
            throw new UTFDataFormatException(
                "encoded string too long: " + utflen + " bytes");
        ...
        ...
}

可以看到使用WriteUTF(String str, DataOutput out) 进行写文件时 可写入字符串的最大字节数为 65535个,当超过65535个字节时就会出现异常。

解决方案:这里贴出我的代码
/**
     *
     * @param fileName 文件名
     * @return
     */
    public File toFile(String fileName) throws IOException {
        File file = new File(fileName);
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
        dos.writeByte(this.logIdf);
        dos.writeByte(this.reserve);
        dos.writeByte(this.logType);
        dos.writeByte(this.logSubType);
        dos.writeInt(getLength());
        if(this.content == null){
            this.content = new JSONObject();
        }
        this.writeUTF(dos, this.content.toJSONString());
        dos.flush();
        dos.close();
        return file;
    }

    private void writeUTF(DataOutputStream outputStream, String content) throws IOException {
        int strlen = content.length();
        int utflen = 0;
        int c = 0;
        int lastIndex = 0;
        int strSize = content.getBytes(StandardCharsets.UTF_8).length;
        int sumSize = 0;
        int bufferSize = 65000; //单次写入字节
        for (int i = 0; i < strlen; i++) {
            c = content.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
            if (utflen >= bufferSize) {
                outputStream.writeUTF(content.substring(lastIndex, i));
                lastIndex = i;
                sumSize += utflen;
                utflen = 0;
            }
        }
        if(sumSize < strSize){
            outputStream.writeUTF(content.substring(lastIndex, strlen));
        }
    }

以上就是本次遇到的问题,以及解决方案。欢迎交流,共同进步。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值