Added test case to test resizing of internal buffer of DataByteArrayOutputStream.

Fix bug during resizing of internal buffer of DataByteArrayOutputStream.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@472258 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian T. Co 2006-11-07 21:00:46 +00:00
parent 0c93dfde72
commit c636b3798f
2 changed files with 108 additions and 7 deletions

View File

@ -138,29 +138,29 @@ public final class DataByteArrayOutputStream extends OutputStream implements Dat
public void writeBoolean(boolean v){
ensureEnoughBuffer(1);
ensureEnoughBuffer(pos + 1);
buf[pos++]=(byte) (v?1:0);
}
public void writeByte(int v){
ensureEnoughBuffer(1);
ensureEnoughBuffer(pos + 1);
buf[pos++]=(byte) (v>>>0);
}
public void writeShort(int v){
ensureEnoughBuffer(2);
ensureEnoughBuffer(pos + 2);
buf[pos++]=(byte) (v>>>8);
buf[pos++]=(byte) (v>>>0);
}
public void writeChar(int v){
ensureEnoughBuffer(2);
ensureEnoughBuffer(pos + 2);
buf[pos++]=(byte) (v>>>8);
buf[pos++]=(byte) (v>>>0);
}
public void writeInt(int v){
ensureEnoughBuffer(4);
ensureEnoughBuffer(pos + 4);
buf[pos++]=(byte) (v>>>24);
buf[pos++]=(byte) (v>>>16);
buf[pos++]=(byte) (v>>>8);
@ -168,7 +168,7 @@ public final class DataByteArrayOutputStream extends OutputStream implements Dat
}
public void writeLong(long v){
ensureEnoughBuffer(8);
ensureEnoughBuffer(pos + 8);
buf[pos++]=(byte) (v>>>56);
buf[pos++]=(byte) (v>>>48);
buf[pos++]=(byte) (v>>>40);
@ -219,7 +219,7 @@ public final class DataByteArrayOutputStream extends OutputStream implements Dat
}
if(encodedsize>65535)
throw new UTFDataFormatException("encoded string too long: "+encodedsize+" bytes");
ensureEnoughBuffer(encodedsize+2);
ensureEnoughBuffer(pos + encodedsize+2);
writeShort(encodedsize);
int i=0;
for(i=0;i<strlen;i++){

View File

@ -0,0 +1,101 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.util;
import junit.framework.TestCase;
import java.io.IOException;
public class DataByteArrayOutputStreamTest extends TestCase {
/**
* This test case assumes that an ArrayIndexOutOfBoundsException will be thrown when the buffer fails to resize
* @throws IOException
*/
public void testResize() throws IOException {
int initSize = 64;
DataByteArrayOutputStream out = new DataByteArrayOutputStream();
fillOut(out, initSize);
// Should resized here
out.writeBoolean(true);
fillOut(out, initSize);
// Should resized here
out.writeByte(1);
fillOut(out, initSize);
// Should resized here
out.writeBytes("test");
fillOut(out, initSize);
// Should resized here
out.writeChar('C');
fillOut(out, initSize);
// Should resized here
out.writeChars("test");
fillOut(out, initSize);
// Should resized here
out.writeDouble(3.1416);
fillOut(out, initSize);
// Should resized here
out.writeFloat((float)3.1416);
fillOut(out, initSize);
// Should resized here
out.writeInt(12345);
fillOut(out, initSize);
// Should resized here
out.writeLong(12345);
fillOut(out, initSize);
// Should resized here
out.writeShort(1234);
fillOut(out, initSize);
// Should resized here
out.writeUTF("test");
fillOut(out, initSize);
// Should resized here
out.write(1234);
fillOut(out, initSize);
// Should resized here
out.write(new byte[10], 5, 5);
fillOut(out, initSize);
// Should resized here
out.write(new byte[10]);
}
/**
* This method restarts the stream to the init size, and fills it up with data
* @param out
* @param size
* @throws IOException
*/
public void fillOut(DataByteArrayOutputStream out, int size) throws IOException {
out.restart(size);
out.write(new byte[size]);
}
}