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

java.io.UTFDataFormatException: encoded string too long: 270976 bytes,第1张

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();
        }
        this.writeUTF(dos, this.content.toJSONString());
        dos.flush();
        dos.close();
        return file;
    }

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/726103.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存