I have been studying Decorator pattern and developed simple class ToUpperCaseInputStream. I overrode read() method so it could convert all chars from InputStream to uppercase. Code of the method is shown below (throws OutOfMemoryError):
@Override
public int read() throws IOException {
return Character.toUpperCase((char)super.read());
}
As I figured out later, casting to char is redundant, but it's not the point. I'm having "java.lang.OutOfMemoryError: Java heap space" when the code:
((char) super.read())
evaluates. To make this simpler I wrote the same method (this one throws OutOfMemoryError):
@Override
public int read() throws IOException {
int c =(char) super.read();
return (c == -1 ? c : Character.toUpperCase(c));
}
And this one does not:
@Override
public int read() throws IOException {
int c = super.read();
return (c == -1 ? c : Character.toUpperCase(c));
}
When I remove casting from the assignment the code runs with no errors and results in all text uppercased. As it's said at Oracle tutorials:
An assignment to an array component of reference type (§15.26.1), a method invocation expression (§15.12), or a prefix or postfix increment (§15.14.2, §15.15.1) or decrement operator (§15.14.3, §15.15.2) may all throw an OutOfMemoryError as a result of boxing conversion (§5.1.7).
It seems that autoboxing is used, but as for me it's not the case. Both variants of the same method result in OutOfMemoryError. If I am wrong, please explain this to me, because it will blow up my head.
To provide more info there is the client code:
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);
}
}
}
What it does is just prints simple message from one file to another.
Although the case is solved I want to share a really good explanation of how casting is done. https://mcmap.net/q/274385/-why-does-39-int-char-byte-2-39-produce-65534-in-java
int
, what did you want to say ? – Rowenprintln
inside thewhile
to see how many times it is being executed in each case. I guess in the second case the while condition always satisfies. – Rowen