List of usage examples for java.io UTFDataFormatException UTFDataFormatException
public UTFDataFormatException(String s)
UTFDataFormatException
with the specified detail message. From source file:UTF8Util.java
/** * Skip characters in the stream./* w w w . j a v a 2 s .c o m*/ * <p> * Note that a smaller number than requested might be skipped if the * end-of-stream is reached before the specified number of characters has * been decoded. It is up to the caller to decide if this is an error * or not. For instance, when determining the character length of a stream, * <code>Long.MAX_VALUE</code> could be passed as the requested number of * characters to skip. * * @param in byte stream with UTF-8 encoded characters * @param charsToSkip the number of characters to skip * @return A long array with counts; the characters skipped at position * <code>CHAR_COUNT</code>, the bytes skipped at position * <code>BYTE_COUNT</code>. Note that the number of characters skipped * may be smaller than the requested number. * @throws IOException if reading from the stream fails * @throws UTFDataFormatException if an invalid UTF-8 encoding is detected */ private static final SkipCount internalSkip(final InputStream in, final long charsToSkip) throws IOException { long charsSkipped = 0; long bytesSkipped = 0; // Decoding routine for modified UTF-8. // See java.io.DataInput while (charsSkipped < charsToSkip) { int c = in.read(); if (c == -1) { break; } charsSkipped++; if ((c & 0x80) == 0x00) { // 8th bit set (top bit) // Found char of one byte width. bytesSkipped++; } else if ((c & 0x60) == 0x40) { // 7th bit set, 6th bit unset // Found char of two byte width. if (InputStreamUtil.skipPersistent(in, 1L) != 1L) { // No second byte present. throw new UTFDataFormatException("Second byte in two byte character missing; byte pos " + bytesSkipped + " ; char pos " + charsSkipped); } bytesSkipped += 2; } else if ((c & 0x70) == 0x60) { // 7th and 6th bit set, 5th unset // Found char of three byte width. int skipped = 0; if (c == 0xe0) { // Check for Derby EOF marker. int c1 = in.read(); int c2 = in.read(); if (c1 == 0x00 && c2 == 0x00) { // Found Derby EOF marker, exit loop. charsSkipped--; // Compensate by subtracting one. break; } // Do some rudimentary error checking. // Allow everything except EOF, which is the same as done in // normal processing (skipPersistent below). if (c1 != -1 && c2 != -1) { skipped = 2; } } else { skipped = (int) InputStreamUtil.skipPersistent(in, 2L); } if (skipped != 2) { // No second or third byte present throw new UTFDataFormatException("Second or third byte in three byte character " + "missing; byte pos " + bytesSkipped + " ; char pos " + charsSkipped); } bytesSkipped += 3; } else { throw new UTFDataFormatException("Invalid UTF-8 encoding encountered: (decimal) " + c); } } // We don't close the stream, since it might be reused. One example of // this is use of Resetable streams. return new SkipCount(charsSkipped, bytesSkipped); }
From source file:com.exadel.flamingo.flex.messaging.amf.io.AMF3Deserializer.java
protected String readAMF3String() throws IOException { String result = null;// w w w . j a v a 2 s.c om if (debugMore) debug("readAMF3String()..."); int type = readAMF3Integer(); if ((type & 0x01) == 0) // stored string result = getFromStoredStrings(type >> 1); else { int length = type >> 1; if (debugMore) debug("readAMF3String() - length=", String.valueOf(length)); if (length > 0) { byte[] utfBytes = new byte[length]; char[] utfChars = new char[length]; readFully(utfBytes); int c, c2, c3, iBytes = 0, iChars = 0; while (iBytes < length) { c = utfBytes[iBytes++] & 0xFF; if (c <= 0x7F) utfChars[iChars++] = (char) c; else { switch (c >> 4) { case 12: case 13: c2 = utfBytes[iBytes++]; if ((c2 & 0xC0) != 0x80) throw new UTFDataFormatException("Malformed input around byte " + (iBytes - 2)); utfChars[iChars++] = (char) (((c & 0x1F) << 6) | (c2 & 0x3F)); break; case 14: c2 = utfBytes[iBytes++]; c3 = utfBytes[iBytes++]; if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80)) throw new UTFDataFormatException("Malformed input around byte " + (iBytes - 3)); utfChars[iChars++] = (char) (((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0)); break; default: throw new UTFDataFormatException("Malformed input around byte " + (iBytes - 1)); } } } result = new String(utfChars, 0, iChars); if (debugMore) debug("readAMF3String() - result=", StringUtil.toString(result)); addToStoredStrings(result); } else result = ""; } if (debugMore) debug("readAMF3String() -> ", StringUtil.toString(result)); return result; }
From source file:com.buaa.cfs.io.UTF8.java
private static void readChars(DataInput in, StringBuilder buffer, int nBytes) throws UTFDataFormatException, IOException { DataOutputBuffer obuf = OBUF_FACTORY.get(); obuf.reset();//ww w .ja va 2 s. c om obuf.write(in, nBytes); byte[] bytes = obuf.getData(); int i = 0; while (i < nBytes) { byte b = bytes[i++]; if ((b & 0x80) == 0) { // 0b0xxxxxxx: 1-byte sequence buffer.append((char) (b & 0x7F)); } else if ((b & 0xE0) == 0xC0) { if (i >= nBytes) { throw new UTFDataFormatException( "Truncated UTF8 at " + StringUtils.byteToHexString(bytes, i - 1, 1)); } // 0b110xxxxx: 2-byte sequence buffer.append((char) (((b & 0x1F) << 6) | (bytes[i++] & 0x3F))); } else if ((b & 0xF0) == 0xE0) { // 0b1110xxxx: 3-byte sequence if (i + 1 >= nBytes) { throw new UTFDataFormatException( "Truncated UTF8 at " + StringUtils.byteToHexString(bytes, i - 1, 2)); } buffer.append((char) (((b & 0x0F) << 12) | ((bytes[i++] & 0x3F) << 6) | (bytes[i++] & 0x3F))); } else if ((b & 0xF8) == 0xF0) { if (i + 2 >= nBytes) { throw new UTFDataFormatException( "Truncated UTF8 at " + StringUtils.byteToHexString(bytes, i - 1, 3)); } // 0b11110xxx: 4-byte sequence int codepoint = ((b & 0x07) << 18) | ((bytes[i++] & 0x3F) << 12) | ((bytes[i++] & 0x3F) << 6) | ((bytes[i++] & 0x3F)); buffer.append(highSurrogate(codepoint)).append(lowSurrogate(codepoint)); } else { // The UTF8 standard describes 5-byte and 6-byte sequences, but // these are no longer allowed as of 2003 (see RFC 3629) // Only show the next 6 bytes max in the error code - in case the // buffer is large, this will prevent an exceedingly large message. int endForError = Math.min(i + 5, nBytes); throw new UTFDataFormatException( "Invalid UTF8 at " + StringUtils.byteToHexString(bytes, i - 1, endForError)); } } }
From source file:UTF8Reader.java
/** Throws an exception for expected byte. */ private void expectedByte(int position, int count) throws UTFDataFormatException { throw new UTFDataFormatException("expectedByte"); }
From source file:UTF8Reader.java
/** Throws an exception for invalid byte. */ private void invalidByte(int position, int count, int c) throws UTFDataFormatException { throw new UTFDataFormatException("invalidByte"); }
From source file:UTF8Reader.java
/** Throws an exception for invalid surrogate bits. */ private void invalidSurrogate(int uuuuu) throws UTFDataFormatException { throw new UTFDataFormatException("invalidHighSurrogate"); }
From source file:org.apache.geode.internal.InternalDataSerializer.java
private static void initializeWellKnownSerializers() { // ArrayBlockingQueue does not have zero-arg constructor // LinkedBlockingQueue does have zero-arg constructor but no way to get capacity classesToSerializers.put("java.lang.String", new WellKnownPdxDS() { @Override/*from ww w .j a v a2 s .c o m*/ public boolean toData(Object o, DataOutput out) throws IOException { try { writeString((String) o, out); } catch (UTFDataFormatException ex) { // See bug 30428 String s = "While writing a String of length " + ((String) o).length(); UTFDataFormatException ex2 = new UTFDataFormatException(s); ex2.initCause(ex); throw ex2; } return true; } }); classesToSerializers.put("java.net.InetAddress", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { InetAddress address = (InetAddress) o; out.writeByte(INET_ADDRESS); writeInetAddress(address, out); return true; } }); classesToSerializers.put("java.net.Inet4Address", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { InetAddress address = (InetAddress) o; out.writeByte(INET_ADDRESS); writeInetAddress(address, out); return true; } }); classesToSerializers.put("java.net.Inet6Address", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { InetAddress address = (InetAddress) o; out.writeByte(INET_ADDRESS); writeInetAddress(address, out); return true; } }); classesToSerializers.put("java.lang.Class", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Class c = (Class) o; if (c.isPrimitive()) { writePrimitiveClass(c, out); } else { out.writeByte(CLASS); writeClass(c, out); } return true; } }); classesToSerializers.put("java.lang.Boolean", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Boolean value = (Boolean) o; out.writeByte(BOOLEAN); writeBoolean(value, out); return true; } }); classesToSerializers.put("java.lang.Character", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Character value = (Character) o; out.writeByte(CHARACTER); writeCharacter(value, out); return true; } }); classesToSerializers.put("java.lang.Byte", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Byte value = (Byte) o; out.writeByte(BYTE); writeByte(value, out); return true; } }); classesToSerializers.put("java.lang.Short", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Short value = (Short) o; out.writeByte(SHORT); writeShort(value, out); return true; } }); classesToSerializers.put("java.lang.Integer", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Integer value = (Integer) o; out.writeByte(INTEGER); writeInteger(value, out); return true; } }); classesToSerializers.put("java.lang.Long", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Long value = (Long) o; out.writeByte(LONG); writeLong(value, out); return true; } }); classesToSerializers.put("java.lang.Float", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Float value = (Float) o; out.writeByte(FLOAT); writeFloat(value, out); return true; } }); classesToSerializers.put("java.lang.Double", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Double value = (Double) o; out.writeByte(DOUBLE); writeDouble(value, out); return true; } }); classesToSerializers.put("[Z", // boolean[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(BOOLEAN_ARRAY); writeBooleanArray((boolean[]) o, out); return true; } }); classesToSerializers.put("[B", // byte[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { byte[] array = (byte[]) o; out.writeByte(BYTE_ARRAY); writeByteArray(array, out); return true; } }); classesToSerializers.put("[C", // char[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(CHAR_ARRAY); writeCharArray((char[]) o, out); return true; } }); classesToSerializers.put("[D", // double[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { double[] array = (double[]) o; out.writeByte(DOUBLE_ARRAY); writeDoubleArray(array, out); return true; } }); classesToSerializers.put("[F", // float[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { float[] array = (float[]) o; out.writeByte(FLOAT_ARRAY); writeFloatArray(array, out); return true; } }); classesToSerializers.put("[I", // int[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { int[] array = (int[]) o; out.writeByte(INT_ARRAY); writeIntArray(array, out); return true; } }); classesToSerializers.put("[J", // long[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { long[] array = (long[]) o; out.writeByte(LONG_ARRAY); writeLongArray(array, out); return true; } }); classesToSerializers.put("[S", // short[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { short[] array = (short[]) o; out.writeByte(SHORT_ARRAY); writeShortArray(array, out); return true; } }); classesToSerializers.put("[Ljava.lang.String;", // String[] new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { String[] array = (String[]) o; out.writeByte(STRING_ARRAY); writeStringArray(array, out); return true; } }); classesToSerializers.put(TimeUnit.NANOSECONDS.getClass().getName(), new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(TIME_UNIT); out.writeByte(TIME_UNIT_NANOSECONDS); return true; } }); classesToSerializers.put(TimeUnit.MICROSECONDS.getClass().getName(), new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(TIME_UNIT); out.writeByte(TIME_UNIT_MICROSECONDS); return true; } }); classesToSerializers.put(TimeUnit.MILLISECONDS.getClass().getName(), new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(TIME_UNIT); out.writeByte(TIME_UNIT_MILLISECONDS); return true; } }); classesToSerializers.put(TimeUnit.SECONDS.getClass().getName(), new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(TIME_UNIT); out.writeByte(TIME_UNIT_SECONDS); return true; } }); classesToSerializers.put("java.util.Date", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Date date = (Date) o; out.writeByte(DATE); writeDate(date, out); return true; } }); classesToSerializers.put("java.io.File", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { File file = (File) o; out.writeByte(FILE); writeFile(file, out); return true; } }); classesToSerializers.put("java.util.ArrayList", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { ArrayList list = (ArrayList) o; out.writeByte(ARRAY_LIST); writeArrayList(list, out); return true; } }); classesToSerializers.put("java.util.LinkedList", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { LinkedList list = (LinkedList) o; out.writeByte(LINKED_LIST); writeLinkedList(list, out); return true; } }); classesToSerializers.put("java.util.Vector", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(VECTOR); writeVector((Vector) o, out); return true; } }); classesToSerializers.put("java.util.Stack", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(STACK); writeStack((Stack) o, out); return true; } }); classesToSerializers.put("java.util.HashSet", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { HashSet list = (HashSet) o; out.writeByte(HASH_SET); writeHashSet(list, out); return true; } }); classesToSerializers.put("java.util.LinkedHashSet", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(LINKED_HASH_SET); writeLinkedHashSet((LinkedHashSet) o, out); return true; } }); classesToSerializers.put("java.util.HashMap", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { HashMap list = (HashMap) o; out.writeByte(HASH_MAP); writeHashMap(list, out); return true; } }); classesToSerializers.put("java.util.IdentityHashMap", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(IDENTITY_HASH_MAP); writeIdentityHashMap((IdentityHashMap) o, out); return true; } }); classesToSerializers.put("java.util.Hashtable", new WellKnownPdxDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(HASH_TABLE); writeHashtable((Hashtable) o, out); return true; } }); classesToSerializers.put("java.util.Properties", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { Properties props = (Properties) o; out.writeByte(PROPERTIES); writeProperties(props, out); return true; } }); classesToSerializers.put("java.util.TreeMap", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(TREE_MAP); writeTreeMap((TreeMap) o, out); return true; } }); classesToSerializers.put("java.util.TreeSet", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(TREE_SET); writeTreeSet((TreeSet) o, out); return true; } }); if (is662SerializationEnabled()) { classesToSerializers.put("java.math.BigInteger", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(BIG_INTEGER); writeBigInteger((BigInteger) o, out); return true; } }); classesToSerializers.put("java.math.BigDecimal", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(BIG_DECIMAL); writeBigDecimal((BigDecimal) o, out); return true; } }); classesToSerializers.put("java.util.UUID", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(UUID); writeUUID((UUID) o, out); return true; } }); classesToSerializers.put("java.sql.Timestamp", new WellKnownDS() { @Override public boolean toData(Object o, DataOutput out) throws IOException { out.writeByte(TIMESTAMP); writeTimestamp((Timestamp) o, out); return true; } }); } }
From source file:org.rdfhdt.hdt.impl.BufferedCompressedStreaming.java
@Override public void insert(TripleString triple) throws DictionaryException, BlockException, UTFDataFormatException { if ((numTriples % this.block_size) == 0) { if (numTriples != 0) try { Iterator<String> subjs = subjectsToProcess.keySet().iterator(); while (subjs.hasNext()) { String currSubj = subjs.next(); processMolecule(subjectsToProcess.get(currSubj)); }/*ww w. j av a 2 s . com*/ currentBlock.save(out); numBlocks++; subjectsToProcess.clear(); } catch (IOException e) { System.err.println("IO exception saving Block"); e.printStackTrace(); } currentBlock = new Block(store_subj_dictionary); } if (triple.getObject().length() < 64 * 1024) { // max:64KB if (subjectsToProcess.containsKey(triple.getSubject().toString())) { TripleString newTriple = new TripleString(triple); subjectsToProcess.get(triple.getSubject().toString()).add(newTriple); // New Subject added to pool } else { ArrayList<TripleString> newTriples = new ArrayList<TripleString>(); TripleString newTriple = new TripleString(triple); newTriples.add(newTriple); subjectsToProcess.put(newTriple.getSubject().toString(), newTriples); // New Subject added to pool } numTriples++; } else { throw new UTFDataFormatException("encoded string too long: " + triple.getObject().length() + " bytes"); } }
From source file:org.rdfhdt.hdt.impl.BufferedCompressedStreamingNoDictionary.java
@Override public void insert(TripleString triple) throws DictionaryException, BlockException, UTFDataFormatException { if ((numTriples % this.block_size) == 0) { if (numTriples != 0) try { Iterator<String> subjs = subjectsToProcess.keySet().iterator(); while (subjs.hasNext()) { String currSubj = subjs.next(); processMolecule(subjectsToProcess.get(currSubj)); }/*w w w .ja v a 2 s . c om*/ currentBlock.save(out); numBlocks++; subjectsToProcess.clear(); } catch (IOException e) { System.err.println("IO exception saving Block"); e.printStackTrace(); } currentBlock = new BlockNoDictionary(store_subj_dictionary); } if (triple.getObject().length() < 64 * 1024) { // max:64KB if (subjectsToProcess.containsKey(triple.getSubject().toString())) { TripleString newTriple = new TripleString(triple); subjectsToProcess.get(triple.getSubject().toString()).add(newTriple); // New Subject added to pool } else { ArrayList<TripleString> newTriples = new ArrayList<TripleString>(); TripleString newTriple = new TripleString(triple); newTriples.add(newTriple); subjectsToProcess.put(newTriple.getSubject().toString(), newTriples); // New Subject added to pool } numTriples++; } else { throw new UTFDataFormatException("encoded string too long: " + triple.getObject().length() + " bytes"); } }