Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
[SANTUARIO-334] - UnsyncByteArrayOutputStream hangs on messages large…
Browse files Browse the repository at this point in the history
…r 512 MB

git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1367492 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
coheigea committed Jul 31, 2012
1 parent fdc48b6 commit cea3c91
Showing 1 changed file with 15 additions and 2 deletions.
Expand Up @@ -45,6 +45,9 @@ public UnsyncByteArrayOutputStream() {
}

public void write(byte[] arg0) {
if ((Integer.MAX_VALUE - pos) < arg0.length) {
throw new OutOfMemoryError();
}
int newPos = pos + arg0.length;
if (newPos > size) {
expandSize(newPos);
Expand All @@ -54,6 +57,9 @@ public void write(byte[] arg0) {
}

public void write(byte[] arg0, int arg1, int arg2) {
if ((Integer.MAX_VALUE - pos) < arg2) {
throw new OutOfMemoryError();
}
int newPos = pos + arg2;
if (newPos > size) {
expandSize(newPos);
Expand All @@ -62,7 +68,10 @@ public void write(byte[] arg0, int arg1, int arg2) {
pos = newPos;
}

public void write(int arg0) {
public void write(int arg0) {
if ((Integer.MAX_VALUE - pos) == 0) {
throw new OutOfMemoryError();
}
int newPos = pos + 1;
if (newPos > size) {
expandSize(newPos);
Expand All @@ -89,7 +98,11 @@ public void close() throws IOException {
private void expandSize(int newPos) {
int newSize = size;
while (newPos > newSize) {
newSize = newSize << 2;
newSize = newSize << 1;
// Deal with overflow
if (newSize < 0) {
newSize = Integer.MAX_VALUE;
}
}
byte newBuf[] = new byte[newSize];
System.arraycopy(buf, 0, newBuf, 0, pos);
Expand Down

0 comments on commit cea3c91

Please sign in to comment.