harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r383871 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src: main/java/java/nio/charset/CharsetEncoder.java test/java/org/apache/harmony/tests/java/nio/charset/CharsetEncoderTest.java
Date Tue, 07 Mar 2006 12:59:24 GMT
Author: tellison
Date: Tue Mar  7 04:59:22 2006
New Revision: 383871

URL: http://svn.apache.org/viewcvs?rev=383871&view=rev
Log:
Fix for HARMONY-170 (java.nio.charset.CharsetEncoder.encode method does not handle the unexpected
exception)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetEncoder.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetEncoderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetEncoder.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetEncoder.java?rev=383871&r1=383870&r2=383871&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetEncoder.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/CharsetEncoder.java
Tue Mar  7 04:59:22 2006
@@ -16,6 +16,8 @@
 package java.nio.charset;
 
 
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 
@@ -440,7 +442,13 @@
 		if (in.remaining() > 0) {
 			while (true) {
 				CodingErrorAction action = null;                
-				result = encodeLoop(in, out);
+				try {
+					result = encodeLoop(in, out);
+				} catch (BufferOverflowException ex) { 
+					throw new CoderMalfunctionError(ex);
+				} catch (BufferUnderflowException ex) {
+					throw new CoderMalfunctionError(ex);
+				}
                 int position = in.position();
 				if (endOfInput && result.isUnderflow() && in.hasRemaining()) {
 					result = CoderResult.malformedForLength(in.remaining());

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetEncoderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetEncoderTest.java?rev=383871&r1=383870&r2=383871&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetEncoderTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetEncoderTest.java
Tue Mar  7 04:59:22 2006
@@ -15,10 +15,13 @@
 
 package org.apache.harmony.tests.java.nio.charset;
 
+import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CoderMalfunctionError;
 import java.nio.charset.CoderResult;
 
 import junit.framework.TestCase;
@@ -68,6 +71,54 @@
 		protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
 			return null;
 		}
+	}
+
+	/*
+	 * Test malfunction encode(CharBuffer)
+	 */
+	public void test_EncodeLjava_nio_CharBuffer() throws Exception {
+		MockMalfunctionCharset cs = new MockMalfunctionCharset("mock", null);
+		try {
+			cs.encode(CharBuffer.wrap("AB"));
+			fail("should throw CoderMalfunctionError");// NON-NLS-1$
+		} catch (CoderMalfunctionError e) {
+			// expected
+		}
+	}
+
+	/*
+	 * Mock charset class with malfunction decode & encode.
+	 */
+	static final class MockMalfunctionCharset extends Charset {
+
+		public MockMalfunctionCharset(String canonicalName, String[] aliases) {
+			super(canonicalName, aliases);
+		}
+
+		public boolean contains(Charset cs) {
+			return false;
+		}
+
+		public CharsetDecoder newDecoder() {
+			return Charset.forName("UTF-8").newDecoder();
+		}
+
+		public CharsetEncoder newEncoder() {
+			return new MockMalfunctionEncoder(this);
+		}
+	}
 
+	/*
+	 * Mock encoder. encodeLoop always throws unexpected exception.
+	 */
+	static class MockMalfunctionEncoder extends java.nio.charset.CharsetEncoder {
+
+		public MockMalfunctionEncoder(Charset cs) {
+			super(cs, 1, 3, new byte[] { (byte) '?' });
+		}
+
+		protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
+			throw new BufferOverflowException();
+		}
 	}
 }



Mime
View raw message