/** * Decode bytes from {@link #byteBuf} and fill {@link #charBuf}. This method * is a no-op if {@link #charBuf} is not empty or if there are no bytes to * decode. * @return true if the buffer contains bytes now, false if it's still empty * @throws CharacterCodingException if the input data contains invalid * characters */ private boolean fillBuffer() throws CharacterCodingException { if (charBuf.hasRemaining()) { return true; } if (byteBuf.position() == 0) { return false; } charBuf.position(0); charBuf.limit(charBuf.capacity()); byteBuf.flip(); CoderResult result = decoder.decode(byteBuf, charBuf, done); if (result.isMalformed()) { throw new MalformedInputException(result.length()); } if (result.isUnmappable()) { throw new UnmappableCharacterException(result.length()); } charBuf.flip(); byteBuf.compact(); return charBuf.hasRemaining(); }
public void testMultiStepEncode() throws CharacterCodingException { encoder.onMalformedInput(CodingErrorAction.REPORT); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); try { encoder.encode(CharBuffer.wrap("\ud800\udc00")); fail("should unmappable"); } catch (UnmappableCharacterException e) { } encoder.reset(); ByteBuffer out = ByteBuffer.allocate(10); assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true) .isMalformed()); encoder.flush(out); encoder.reset(); out = ByteBuffer.allocate(10); assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer .wrap("\ud800"), out, false)); assertTrue(encoder.encode(CharBuffer.wrap("\udc00"), out, true) .isMalformed()); }
@Override public Appendable append(CharSequence csq, int start, int end) throws IOException { if (csq.length() < end) { throw new IndexOutOfBoundsException(); } if (csq instanceof ReadOnlyAsciiBuf) { content().writeBytes(((ReadOnlyAsciiBuf) csq).content()); } else { for (int i = start; i < end; i++) { char c = csq.charAt(i); if (c >= '\u0080') { throw new UnmappableCharacterException(2); } content().writeByte((byte) c); } } return this; }
public void testIncompatibleCharacter() throws Exception { DataObject d = DataObject.find(testFileObject); encodingName = "ISO-8859-1"; // NOI18N EditorCookie o = d.getLookup().lookup(EditorCookie.class); StyledDocument doc = o.openDocument(); doc.insertString(0, CZECH_STRING_UTF, null); try { o.saveDocument(); // try to open the file InputStream istm = testFileObject.getInputStream(); try { BufferedReader r = new BufferedReader(new InputStreamReader(istm, "ISO-8859-2")); // NOI18N String line = r.readLine(); int questionMarkPos = line.indexOf('?'); // NOI18N assertTrue("Should save question marks", questionMarkPos != -1); // NOI18N } finally { istm.close(); } //fail("Exception expected"); } catch (UnmappableCharacterException ex) { // expected exceptiom } }
private void convertCharArray() { try { // Possible optimization of directly converting into the CDR buffer. // However, that means the CDR code would have to reserve // a 4 byte string length ahead of time, and we'd need a // confusing partial conversion scheme for when we couldn't // fit everything in the buffer but needed to know the // converted length before proceeding due to fragmentation. // Then there's the issue of the chunking code. // // For right now, this is less messy and basic tests don't // show more than a 1 ms penalty worst case. Less than a // factor of 2 increase. // Convert the characters buffer = ctb.encode(CharBuffer.wrap(chars,0,numChars)); // ByteBuffer returned by the encoder will set its limit // to byte immediately after the last written byte. numBytes = buffer.limit(); } catch (IllegalStateException ise) { // an encoding operation is already in progress throw wrapper.ctbConverterFailure( ise ) ; } catch (MalformedInputException mie) { // There were illegal Unicode char pairs throw wrapper.badUnicodePair( mie ) ; } catch (UnmappableCharacterException uce) { // A character doesn't map to the desired code set // CORBA formal 00-11-03. throw omgWrapper.charNotInCodeset( uce ) ; } catch (CharacterCodingException cce) { // If this happens, then some other encoding error occured throw wrapper.ctbConverterFailure( cce ) ; } }
/** * Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...) */ public void testWriteLines() throws IOException { // zero lines Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII); assert(Files.size(tmpfile) == 0); assert(result == tmpfile); // two lines List<String> lines = Arrays.asList("hi", "there"); Files.write(tmpfile, lines, US_ASCII); List<String> actual = Files.readAllLines(tmpfile, US_ASCII); assertTrue(actual.equals(lines), "Unexpected lines"); // append two lines Files.write(tmpfile, lines, US_ASCII, APPEND); List<String> expected = new ArrayList<>(); expected.addAll(lines); expected.addAll(lines); assertTrue(expected.size() == 4, "List should have 4 elements"); actual = Files.readAllLines(tmpfile, US_ASCII); assertTrue(actual.equals(expected), "Unexpected lines"); // UnmappableCharacterException try { String s = "\u00A0\u00A1"; Files.write(tmpfile, Arrays.asList(s), US_ASCII); fail("UnmappableCharacterException expected"); } catch (UnmappableCharacterException ignore) { } }
/** * Tests if the feeder throws an exception if one of the output characters * is unmappable * @throws CharacterCodingException if the test is successful */ @Test(expected = UnmappableCharacterException.class) public void unmappableCharacter() throws CharacterCodingException { DefaultJsonFeeder feeder = new DefaultJsonFeeder( Charset.forName("IBM1098")); feeder.feed((byte)0x80); feeder.feed((byte)0x81); while (feeder.hasInput()) { System.out.println(feeder.nextInput()); } }
private SQLException badRepresentation(Exception e) { if ( e instanceof NullPointerException ) return new SQLNonTransientException( "attempted write via SQLOutput after closing it", "55000", e); if ( e instanceof BufferOverflowException ) return new SQLNonTransientException( "Byte limit exceeded for user-defined type", "54000"); if ( e instanceof UnmappableCharacterException ) /* * As long as the string encoding is unconditionally UTF-8, this * shouldn't really be possible, but if the encoding is ever made * selectable, this could happen. */ return new SQLDataException( "Character not available in destination encoding", "22P05", e); if ( e instanceof MalformedInputException ) /* * This actually CAN happen ... as the input arrives in UTF-16, * not codepoints. */ return new SQLDataException( "Input that does not encode a valid character", "22021", e); return new SQLDataException( "Could not form binary representation of user-defined type", "22P03", e); }
/** * Append data to the window (i.e. make it larger) * @param buf the data to append */ public void append(Buffer buf) { // append new bytes to buffered bytes or use them directly if (this.buf.length() > 0) { this.buf.appendBuffer(buf); } else { this.buf = buf; } // convert Vert.x buffer to ByteBuffer (ugly!) ByteBuffer byteBuf = ByteBuffer.wrap(this.buf.getBytes()); // prepare temporary CharBuffer ensureCharBuffer(buf.length()); charBuf.position(0); charBuf.limit(charBuf.capacity()); // decode ByteBuffer to temporary CharBuffer CoderResult result = decoder.decode(byteBuf, charBuf, false); if (result.isMalformed()) { throw new IllegalStateException( new MalformedInputException(result.length())); } if (result.isUnmappable()) { throw new IllegalStateException( new UnmappableCharacterException(result.length())); } // reset CharBuffer and remove decoded bytes from byte buffer charBuf.flip(); this.buf = this.buf.getBuffer(byteBuf.position(), this.buf.length()); // append to decoded string buffer this.decodedBuf.append(charBuf); }
/** Calculate a chunk array from an encoded string */ static private byte[] chunk(StringBuilder src) throws IOException { int c = 0; int s = 0; int i = 1; for (; s < src.length(); s++) { int v = getValue(src.charAt(s)); if (v == UNMAPPED) throw new UnmappableCharacterException(0); if (v >= 0) { c |= v << (24 - i * 6); if (i > 4) break; i++; } } src.delete(0, s); int l = Math.max(0, i - 2); byte[] b = new byte[l]; if (l > 0) b[0] = (byte)((c >> 16) & 0xFF); if (l > 1) b[1] = (byte)((c >> 8) & 0xFF); if (l > 2) b[2] = (byte)(c & 0xFF); return b; }
public void assertDeserialized(Serializable initial, Serializable deserialized) { // do common checks for all throwable objects SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial, deserialized); UnmappableCharacterException initEx = (UnmappableCharacterException) initial; UnmappableCharacterException desrEx = (UnmappableCharacterException) deserialized; assertEquals("InputLength", initEx.getInputLength(), desrEx .getInputLength()); }
/** * @tests serialization/deserialization compatibility. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization.", method = "!SerializationSelf", args = {} ) public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new UnmappableCharacterException(11), COMPARATOR); }
/** * @tests serialization/deserialization compatibility with RI. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization.", method = "!SerializationGolden", args = {} ) public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new UnmappableCharacterException( 11), COMPARATOR); }
@TestTargets({ @TestTargetNew( level = TestLevel.PARTIAL, notes = "Exceptions checking missed.", method = "encode", args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class} ), @TestTargetNew( level = TestLevel.PARTIAL, notes = "Exceptions checking missed.", method = "encode", args = {java.nio.CharBuffer.class} ) }) public void testMultiStepEncode() throws CharacterCodingException { encoder.onMalformedInput(CodingErrorAction.REPORT); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); try { encoder.encode(CharBuffer.wrap("\ud800\udc00")); fail("should unmappable"); } catch (UnmappableCharacterException e) { } encoder.reset(); ByteBuffer out = ByteBuffer.allocate(10); assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true) .isMalformed()); encoder.flush(out); encoder.reset(); out = ByteBuffer.allocate(10); assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer .wrap("\ud800"), out, false)); assertTrue(encoder.encode(CharBuffer.wrap("\udc00"), out, true) .isMalformed()); }
public static ReadableCharBuf ascii(char[] values, int start, int length) { byte[] data = new byte[length]; for (int i = start; i < (start + length); i++) { char c = values[i]; if (c >= '\u0080') { throw new RuntimeException(new UnmappableCharacterException(2)); } data[i] = (byte) c; } return new ByteArrayReadOnlyAsciiBuf(data); }
@Override public Appendable append(char c) throws IOException { if (c >= '\u0080') { throw new UnmappableCharacterException(2); } content().writeByte((byte) c); return this; }