Author: markt
Date: Tue Dec 5 22:37:33 2017
New Revision: 1817248
URL: http://svn.apache.org/viewvc?rev=1817248&view=rev
Log:
Fix SpotBugs warning (rank <=16)
serialization for ByteChunk
Modified:
tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
tomcat/trunk/test/org/apache/tomcat/util/buf/TestByteChunk.java
Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java?rev=1817248&r1=1817247&r2=1817248&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java Tue Dec 5
22:37:33 2017
@@ -17,6 +17,8 @@
package org.apache.tomcat.util.buf;
import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
@@ -127,7 +129,7 @@ public final class ByteChunk implements
private int start=0;
private int end;
- private Charset charset;
+ private transient Charset charset;
private boolean isSet=false; // XXX
@@ -149,6 +151,19 @@ public final class ByteChunk implements
allocate( initial, -1 );
}
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {
+ oos.defaultWriteObject();
+ oos.writeUTF(getCharset().name());
+ }
+
+
+ private void readObject(ObjectInputStream ois) throws
ClassNotFoundException, IOException {
+ ois.defaultReadObject();
+ this.charset = Charset.forName(ois.readUTF());
+ }
+
+
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
@@ -799,6 +814,7 @@ public final class ByteChunk implements
return (ret >= start) ? ret - start : -1;
}
+
/**
* Returns the first instance of the given character in the given byte
array
* between the specified start and end.
Modified: tomcat/trunk/test/org/apache/tomcat/util/buf/TestByteChunk.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/buf/TestByteChunk.java?rev=1817248&r1=1817247&r2=1817248&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/util/buf/TestByteChunk.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/util/buf/TestByteChunk.java Tue Dec 5
22:37:33 2017
@@ -16,7 +16,12 @@
*/
package org.apache.tomcat.util.buf;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Assert;
@@ -133,4 +138,26 @@ public class TestByteChunk {
'e' }));
Assert.assertEquals(-1, ByteChunk.findBytes(bytes, 2, 5, new byte[] {
'w' }));
}
+
+ @Test
+ public void testSerialization() throws Exception {
+ String data = "Hello world!";
+ byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
+
+ ByteChunk bcIn = new ByteChunk();
+ bcIn.setBytes(bytes, 0, bytes.length);
+ bcIn.setCharset(StandardCharsets.UTF_8);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(bcIn);
+ oos.close();
+
+ ByteArrayInputStream bais = new
ByteArrayInputStream(baos.toByteArray());
+ ObjectInputStream ois = new ObjectInputStream(bais);
+ ByteChunk bcOut = (ByteChunk) ois.readObject();
+
+ Assert.assertArrayEquals(bytes, bcOut.getBytes());
+ Assert.assertEquals(bcIn.getCharset(), bcOut.getCharset());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]