Skip to content
Permalink
Browse files

Improved: Remove useless ‘UtilObject#getBytes’

(OFBIZ-11140)


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1863444 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 19, 2019
1 parent cc85af1 commit 1427c576d5491569b3d9eda4ba581f8f2885d6b4
@@ -21,7 +21,6 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.Iterator;
@@ -41,21 +40,6 @@ private UtilObject() {

public static final String module = UtilObject.class.getName();

public static byte[] getBytes(InputStream is) {
byte[] buffer = new byte[4 * 1024];
byte[] data = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()){
int numBytesRead;
while ((numBytesRead = is.read(buffer)) != -1) {
bos.write(buffer, 0, numBytesRead);
}
data = bos.toByteArray();
} catch (IOException e) {
Debug.logError(e, module);
}
return data;
}

/** Serialize an object to a byte array */
public static byte[] getBytes(Object obj) {
byte[] data = null;
@@ -156,31 +156,6 @@ public void testErrorInjector() throws Exception {
in.close();
}

public void testGetBytes_Stream() {
boolean errorOn = Debug.isOn(Debug.ERROR);
try {
Debug.set(Debug.ERROR, false);
byte[] source = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
byte[] result = UtilObject.getBytes(new ByteArrayInputStream(source));
assertNotNull("initial result", result);
assertEquals("initial equals", source, result);
assertNull("error after read", UtilObject.getBytes(new ErrorInjector(new ByteArrayInputStream(source), 3)));
byte[] closeResult = UtilObject.getBytes(new ErrorInjector(new ByteArrayInputStream(source), true));
assertNotNull("error on close", closeResult);
assertEquals("error on close equals", source, result);
Exception caught = null;
try {
UtilObject.getBytes(null);
} catch (NullPointerException e) {
caught = e;
} finally {
assertNotNull("null stream exception", caught);
}
} finally {
Debug.set(Debug.ERROR, errorOn);
}
}

@SuppressWarnings("serial")
public static class SerializationInjector implements Serializable {
private boolean onRead;

0 comments on commit 1427c57

Please sign in to comment.
You can’t perform that action at this time.