OutOfMemoryError:将数字基元转换为char时的Java堆空间

Zar*_*ial 8 java primitive autoboxing casting out-of-memory

我一直在研究Decorator模式并开发了简单的类ToUpperCaseInputStream.我重写了read()方法,因此它可以将所有字符从InputStream转换为大写.该方法的代码如下所示(抛出OutOfMemoryError):

@Override
public int read() throws IOException {
    return Character.toUpperCase((char)super.read());
}
Run Code Online (Sandbox Code Playgroud)

正如我后面所说的,转换为char是多余的,但这不是重点.当代码时我有"java.lang.OutOfMemoryError:Java堆空间":

((char) super.read())  
Run Code Online (Sandbox Code Playgroud)

评估.为了使这更简单,我写了相同的方法(这个抛出OutOfMemoryError):

@Override
public int read() throws IOException {
    int c =(char) super.read();
    return (c == -1 ? c : Character.toUpperCase(c));
} 
Run Code Online (Sandbox Code Playgroud)

而这个不是:

@Override
public int read() throws IOException {
    int c = super.read();
    return (c == -1 ? c : Character.toUpperCase(c));
} 
Run Code Online (Sandbox Code Playgroud)

当我从赋值中删除转换时,代码运行时没有错误,并导致所有文本都是大写的.正如在Oracle教程中所说:

赋值参考类型的数组组分(§15.26.1),一个方法调用表达式(§15.12),或前缀或后缀增量(§15.14.2,§15.15.1)或递减运算符(§15.14.3 ,§15.15.2)可能所有抛出一个OutOfMemoryError拳击变换的结果 (§5.1.7).

似乎使用了自动装箱,但对我来说并非如此.同一方法的两种变体都会导致OutOfMemoryError.如果我错了,请向我解释一下,因为它会炸毁我的头脑.

要提供更多信息,请提供客户端代码:

public class App {
public static void main(String[] args) throws IOException {

    try (InputStream inet = new ToUpperCaseInputStream(new FileInputStream("d:/TEMP/src.txt"));
        FileOutputStream buff = new FileOutputStream("d:/TEMP/dst.txt")) {
        copy(inet, buff);
    }
}

public static void copy(InputStream src, OutputStream dst) throws IOException {
    int elem;
    while ((elem = src.read()) != -1) {
        dst.write(elem);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

它的作用是将简单的消息从一个文件打印到另一个文件.

虽然案例已经解决,但我想分享一个关于如何完成铸造的非常好的解释./sf/answers/1724633711/

yur*_*gis 4

在转换为 char 之前,您需要检查 -1(表示输入数据结束)。

Java 中的 char 是一个无符号短整型,这意味着当返回 -1 时,您的转换将使其变为 65535。即使您没有 OutOfMemory,您的代码仍然损坏。

关于为什么会出现 OOM 错误,没有完整的代码很难说,也许,在代码的后面有一些基于字符值的内存分配。

所以尝试一下这个,看看是否有帮助:

@Override
public int read() throws IOException {
    int c = super.read();
    if (c == -1) return c;

    char ch = (char) c;
    return Character.toUpperCase(ch);
} 
Run Code Online (Sandbox Code Playgroud)

  • 是的,试试这个代码 { char c = 0; C - ; System.out.println(c); 有关无符号值的更多信息:http://en.wikipedia.org/wiki/Signedness (2认同)