test/sun/nio/cs/NIOJISAutoDetectTest.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4831163 5053096 5056440
  27  * @summary NIO charset basic verification of JISAutodetect decoder
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.io.*;
  32 import java.nio.ByteBuffer;
  33 import java.nio.CharBuffer;
  34 import java.nio.charset.Charset;
  35 import java.nio.charset.CharsetDecoder;
  36 import java.nio.charset.CoderResult;
  37 import static java.lang.System.*;
  38 
  39 public class NIOJISAutoDetectTest {
  40     private static int failures = 0;
  41 
  42     private static void fail(String failureMsg) {
  43         System.out.println(failureMsg);
  44         failures++;
  45     }
  46 


 222             CharBuffer cb = CharBuffer.allocate(128);
 223             bb.put((byte)'A').put((byte)0x8f);
 224             bb.flip();
 225             CoderResult res = dc.decode(bb,cb,false);
 226             check(res.isUnderflow(), "isUnderflow");
 227             check(bb.position() == 1, "bb.position()");
 228             check(cb.position() == 1, "cb.position()");
 229             res = dc.decode(bb,cb,false);
 230             check(res.isUnderflow(), "isUnderflow");
 231             check(bb.position() == 1, "bb.position()");
 232             check(cb.position() == 1, "cb.position()");
 233             bb.compact();
 234             bb.put((byte)0xa1);
 235             bb.flip();
 236             res = dc.decode(bb,cb,true);
 237             check(res.isUnderflow(), "isUnderflow");
 238             check(bb.position() == 2, "bb.position()");
 239             check(cb.position() == 2, "cb.position()");
 240         }
 241 








 242 
 243         if (failures > 0)
 244             throw new RuntimeException(failures + " tests failed");
 245     }
 246 
 247     static void checkCoderResult(CoderResult result) {
 248         check(result.isUnderflow(),
 249               "Unexpected coder result: " + result);
 250     }
 251 
 252     static void test(String expectedCharset, byte[] input) throws Exception {
 253         Charset cs = Charset.forName("x-JISAutoDetect");
 254         CharsetDecoder autoDetect = cs.newDecoder();
 255 
 256         Charset cs2 = Charset.forName(expectedCharset);
 257         CharsetDecoder decoder = cs2.newDecoder();
 258 
 259         ByteBuffer bb = ByteBuffer.allocate(128);
 260         CharBuffer charOutput = CharBuffer.allocate(128);
 261         CharBuffer charExpected = CharBuffer.allocate(128);




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4831163 5053096 5056440 8022224
  27  * @summary NIO charset basic verification of JISAutodetect decoder
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.io.*;
  32 import java.nio.ByteBuffer;
  33 import java.nio.CharBuffer;
  34 import java.nio.charset.Charset;
  35 import java.nio.charset.CharsetDecoder;
  36 import java.nio.charset.CoderResult;
  37 import static java.lang.System.*;
  38 
  39 public class NIOJISAutoDetectTest {
  40     private static int failures = 0;
  41 
  42     private static void fail(String failureMsg) {
  43         System.out.println(failureMsg);
  44         failures++;
  45     }
  46 


 222             CharBuffer cb = CharBuffer.allocate(128);
 223             bb.put((byte)'A').put((byte)0x8f);
 224             bb.flip();
 225             CoderResult res = dc.decode(bb,cb,false);
 226             check(res.isUnderflow(), "isUnderflow");
 227             check(bb.position() == 1, "bb.position()");
 228             check(cb.position() == 1, "cb.position()");
 229             res = dc.decode(bb,cb,false);
 230             check(res.isUnderflow(), "isUnderflow");
 231             check(bb.position() == 1, "bb.position()");
 232             check(cb.position() == 1, "cb.position()");
 233             bb.compact();
 234             bb.put((byte)0xa1);
 235             bb.flip();
 236             res = dc.decode(bb,cb,true);
 237             check(res.isUnderflow(), "isUnderflow");
 238             check(bb.position() == 2, "bb.position()");
 239             check(cb.position() == 2, "cb.position()");
 240         }
 241 
 242         // test #8022224
 243         Charset cs = Charset.forName("x-JISAutoDetect");
 244         ByteBuffer bb = ByteBuffer.wrap(new byte[] { 'a', 0x1b, 0x24, 0x40 });
 245         CharBuffer cb = CharBuffer.wrap(new char[10]);
 246         CoderResult cr = cs.newDecoder().decode(bb, cb, false);
 247         bb.rewind();
 248         cb.clear().limit(1);
 249         check(cr == cs.newDecoder().decode(bb, cb, false), "#8022224");
 250 
 251         if (failures > 0)
 252             throw new RuntimeException(failures + " tests failed");
 253     }
 254 
 255     static void checkCoderResult(CoderResult result) {
 256         check(result.isUnderflow(),
 257               "Unexpected coder result: " + result);
 258     }
 259 
 260     static void test(String expectedCharset, byte[] input) throws Exception {
 261         Charset cs = Charset.forName("x-JISAutoDetect");
 262         CharsetDecoder autoDetect = cs.newDecoder();
 263 
 264         Charset cs2 = Charset.forName(expectedCharset);
 265         CharsetDecoder decoder = cs2.newDecoder();
 266 
 267         ByteBuffer bb = ByteBuffer.allocate(128);
 268         CharBuffer charOutput = CharBuffer.allocate(128);
 269         CharBuffer charExpected = CharBuffer.allocate(128);