當前位置: 首頁>>代碼示例>>Java>>正文


Java UTFDataFormatException類代碼示例

本文整理匯總了Java中java.io.UTFDataFormatException的典型用法代碼示例。如果您正苦於以下問題:Java UTFDataFormatException類的具體用法?Java UTFDataFormatException怎麽用?Java UTFDataFormatException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


UTFDataFormatException類屬於java.io包,在下文中一共展示了UTFDataFormatException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: byteBuf2String

import java.io.UTFDataFormatException; //導入依賴的package包/類
public static String byteBuf2String(ByteBuf buf, Charset charset) throws UTFDataFormatException, IndexOutOfBoundsException, CharacterCodingException {

        int byteLen = buf.readableBytes();

        if (charset.equals(StandardCharsets.US_ASCII)) {
            return Utf8Reader.readUtf8(buf, byteLen);
        } else if (charset.equals(StandardCharsets.UTF_8)) {
            try {
                return Utf8Reader.readUtf8(buf.duplicate(), (int) (byteLen * 1.4));
            } catch (IndexOutOfBoundsException e) {
                // try again with 3 bytes per char
                return Utf8Reader.readUtf8(buf, byteLen * 3);
            }
        } else {
            return byteBuffersToString(buf.nioBuffers(), charset);
        }
    }
 
開發者ID:amaralDaniel,項目名稱:megaphone,代碼行數:18,代碼來源:ByteBufUtils.java

示例2: countBytes

import java.io.UTFDataFormatException; //導入依賴的package包/類
/**
 * Returns the number of bytes the modified UTF8 representation of 's' would take.
 */
public static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {
    long result = 0;
    final int length = s.length();
    for (int i = 0; i < length; ++i) {
        char ch = s.charAt(i);
        if (ch != 0 && ch <= 127) { // U+0000 uses two bytes.
            ++result;
        } else if (ch <= 2047) {
            result += 2;
        } else {
            result += 3;
        }
        if (shortLength && result > 65535) {
            throw new UTFDataFormatException("String more than 65535 UTF bytes long");
        }
    }
    return result;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:22,代碼來源:Mutf8.java

示例3: writeUTF

import java.io.UTFDataFormatException; //導入依賴的package包/類
public void writeUTF(String str) throws IOException {

        int len = str.length();

        if (len > 0xffff) {
            throw new UTFDataFormatException();
        }

        int bytecount = StringConverter.getUTFSize(str);

        if (bytecount > 0xffff) {
            throw new UTFDataFormatException();
        }

        //
        writeChar(bytecount);

        HsqlByteArrayOutputStream bao =
            new HsqlByteArrayOutputStream(bytecount);

        StringConverter.stringToUTFBytes(str, bao);
        this.write(bao.getBuffer(), 0, bao.size());
    }
 
開發者ID:tiweGH,項目名稱:OpenDiabetes,代碼行數:24,代碼來源:DataOutputStream.java

示例4: readString

import java.io.UTFDataFormatException; //導入依賴的package包/類
public String readString() {
    int offset = readInt();
    int savedPosition = data.position();
    int savedLimit = data.limit();
    data.position(offset);
    data.limit(data.capacity());
    try {
        int expectedLength = readUleb128();
        String result = Mutf8.decode(this, new char[expectedLength]);
        if (result.length() != expectedLength) {
            throw new DexException("Declared length " + expectedLength
                    + " doesn't match decoded length of " + result.length());
        }
        return result;
    } catch (UTFDataFormatException e) {
        throw new DexException(e);
    } finally {
        data.position(savedPosition);
        data.limit(savedLimit);
    }
}
 
開發者ID:alibaba,項目名稱:atlas,代碼行數:22,代碼來源:Dex.java

示例5: countBytes

import java.io.UTFDataFormatException; //導入依賴的package包/類
/**
 * Returns the number of bytes the modified UTF8 representation of 's' would take.
 */
private static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {
    long result = 0;
    final int length = s.length();
    for (int i = 0; i < length; ++i) {
        char ch = s.charAt(i);
        if (ch != 0 && ch <= 127) { // U+0000 uses two bytes.
            ++result;
        } else if (ch <= 2047) {
            result += 2;
        } else {
            result += 3;
        }
        if (shortLength && result > 65535) {
            throw new UTFDataFormatException("String more than 65535 UTF bytes long");
        }
    }
    return result;
}
 
開發者ID:alibaba,項目名稱:atlas,代碼行數:22,代碼來源:Mutf8.java

示例6: readString

import java.io.UTFDataFormatException; //導入依賴的package包/類
public String readString() {
    int offset = readInt();
    int savedPosition = data.position();
    int savedLimit = data.limit();
    data.position(offset);
    data.limit(data.capacity());
    try {
        int expectedLength = readUleb128();
        String result = Mutf8.decode(this, new char[expectedLength]);
        if (result.length() != expectedLength) {
            throw new DexException2("Declared length " + expectedLength + " doesn't match decoded length of "
                    + result.length());
        }
        return result;
    } catch (UTFDataFormatException e) {
        throw new DexException2(e);
    } finally {
        data.position(savedPosition);
        data.limit(savedLimit);
    }
}
 
開發者ID:alibaba,項目名稱:atlas,代碼行數:22,代碼來源:Dex.java

示例7: writeQuickFullUTF

import java.io.UTFDataFormatException; //導入依賴的package包/類
/**
 * Used when we know the max size will fit in the current buffer.
 */
private final void writeQuickFullUTF(String str, int strlen) throws IOException {
  int utfSizeIdx = this.buffer.position();
  // skip bytes reserved for length
  this.buffer.position(utfSizeIdx + 2);
  for (int i = 0; i < strlen; i++) {
    int c = str.charAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
      this.buffer.put((byte) c);
    } else if (c > 0x07FF) {
      this.buffer.put((byte) (0xE0 | ((c >> 12) & 0x0F)));
      this.buffer.put((byte) (0x80 | ((c >> 6) & 0x3F)));
      this.buffer.put((byte) (0x80 | ((c >> 0) & 0x3F)));
    } else {
      this.buffer.put((byte) (0xC0 | ((c >> 6) & 0x1F)));
      this.buffer.put((byte) (0x80 | ((c >> 0) & 0x3F)));
    }
  }
  int utflen = this.buffer.position() - (utfSizeIdx + 2);
  if (utflen > 65535) {
    // act as if we wrote nothing to this buffer
    this.buffer.position(utfSizeIdx);
    throw new UTFDataFormatException();
  }
  this.buffer.putShort(utfSizeIdx, (short) utflen);
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:29,代碼來源:MsgStreamer.java

示例8: countBytes

import java.io.UTFDataFormatException; //導入依賴的package包/類
public static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {
    long result = 0;
    int length = s.length();
    int i = 0;
    while (i < length) {
        char ch = s.charAt(i);
        if (ch != '\u0000' && ch <= '') {
            result++;
        } else if (ch <= '߿') {
            result += 2;
        } else {
            result += 3;
        }
        if (!shortLength || result <= 65535) {
            i++;
        } else {
            throw new UTFDataFormatException("String more than 65535 UTF bytes long");
        }
    }
    return result;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:22,代碼來源:Mutf8.java

示例9: testSkipFullyOnInvalidStreamCJK

import java.io.UTFDataFormatException; //導入依賴的package包/類
/**
 * Tests that <code>skipFully</code> throws exception if there is a UTF-8
 * encoding error in the stream
 * 
 * @throws IOException if the test fails for some unexpected reason
 */
public void testSkipFullyOnInvalidStreamCJK()
        throws IOException {
    final int charLength = 10;
    InputStream in = new ReaderToUTF8Stream(
            new LoopingAlphabetReader(charLength, CharAlphabet.cjkSubset()),
            charLength, 0, "ignored-test-type");
    in.skip(2L); // Skip encoded length added by ReaderToUTF8Stream.
    in.skip(1L); // Skip one more byte to trigger a UTF error.
    try {
        UTF8Util.skipFully(in, charLength);
        fail("Should have failed because of UTF error.");
    } catch (UTFDataFormatException udfe) {
        // As expected, do nothing.
    }
}
 
開發者ID:gemxd,項目名稱:gemfirexd-oss,代碼行數:22,代碼來源:UTF8UtilTest.java

示例10: testSkippingInvalidEncodingWorks

import java.io.UTFDataFormatException; //導入依賴的package包/類
/**
 * Demonstrates that skipping incorrectly encoded character sequences
 * works because the stream is not checked for well-formedness.
 */
public void testSkippingInvalidEncodingWorks()
        throws IOException {
    // The array contains three valid characters and one invalid three-byte
    // representation that only has two bytes present.
    // When skipping, this sequence is (incorrectly) taken as a sequence of
    // three characters ('a' - some three byte character - 'a').
    // 0xef = 11101111, 0xb8 = 10111000
    byte[] data = {'a', (byte)0xef, (byte)0xb8, 'a', 'a'};
    byte[] dataWithLength =
        {0x0, 0x5, 'a', (byte)0xef, (byte)0xb8, 'a', 'a'};
    InputStream is = new ByteArrayInputStream(data);
    // This is actually incorrect, but does work currently.
    UTF8Util.skipFully(is, 3);
    // Verify that decoding this actually fails.
    DataInputStream dis = new DataInputStream(
                                new ByteArrayInputStream(dataWithLength));
    try {
        dis.readUTF();
        fail("UTF-8 expected to be invalid, read should fail");
    } catch (UTFDataFormatException udfe) {
        // This is expected, since the UTF-8 encoding is invalid
    }
}
 
開發者ID:gemxd,項目名稱:gemfirexd-oss,代碼行數:28,代碼來源:UTF8UtilTest.java

示例11: writeUTF

import java.io.UTFDataFormatException; //導入依賴的package包/類
@Override
public void writeUTF(String str) throws IOException {
    int utfCount = 0, length = str.length();
    for (int i = 0; i < length; i++) {
        int charValue = str.charAt(i);
        if (charValue > 0 && charValue <= 127) {
            utfCount++;
        } else if (charValue <= 2047) {
            utfCount += 2;
        } else {
            utfCount += 3;
        }
    }
    if (utfCount > 65535) {
        throw new UTFDataFormatException(); //$NON-NLS-1$
    }
    position += utfCount * 2;
}
 
開發者ID:instaclustr,項目名稱:cassandra-sstable-tools,代碼行數:19,代碼來源:PurgeStatisticBackend.java

示例12: readString

import java.io.UTFDataFormatException; //導入依賴的package包/類
public String readString() {
    int offset = readInt();
    int savedPosition = position;
    position = offset;
    try {
        int expectedLength = readUleb128();
        String result = Mutf8.decode(this, new char[expectedLength]);
        if (result.length() != expectedLength) {
            throw new DexException("Declared length " + expectedLength
                    + " doesn't match decoded length of " + result.length());
        }
        return result;
    } catch (UTFDataFormatException e) {
        throw new DexException(e);
    } finally {
        position = savedPosition;
    }
}
 
開發者ID:AndreJCL,項目名稱:JCL,代碼行數:19,代碼來源:DexBuffer.java

示例13: decode

import java.io.UTFDataFormatException; //導入依賴的package包/類
@Override
public Instant decode(InputStream inStream) throws CoderException, IOException {
  long shiftedMillis;
  try {
    shiftedMillis = new DataInputStream(inStream).readLong();
  } catch (EOFException | UTFDataFormatException exn) {
    // These exceptions correspond to decoding problems, so change
    // what kind of exception they're branded as.
    throw new CoderException(exn);
  }

  // Produces an {@link Instant} from a {@code long} representing its millis-since-epoch,
  // but shifted so that the byte representation of negative values are lexicographically
  // ordered before the byte representation of positive values.
  //
  // This deliberately utilizes the well-defined overflow for {@code long} values.
  // See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.2
  return new Instant(shiftedMillis + Long.MIN_VALUE);
}
 
開發者ID:apache,項目名稱:beam,代碼行數:20,代碼來源:InstantCoder.java

示例14: decode

import java.io.UTFDataFormatException; //導入依賴的package包/類
@Override
public String decode(InputStream inStream, Context context)
    throws IOException {
  if (context.isWholeStream) {
    byte[] bytes = StreamUtils.getBytes(inStream);
    return new String(bytes, StandardCharsets.UTF_8);
  } else {
    try {
      return readString(new DataInputStream(inStream));
    } catch (EOFException | UTFDataFormatException exn) {
      // These exceptions correspond to decoding problems, so change
      // what kind of exception they're branded as.
      throw new CoderException(exn);
    }
  }
}
 
開發者ID:apache,項目名稱:beam,代碼行數:17,代碼來源:StringUtf8Coder.java

示例15: decode

import java.io.UTFDataFormatException; //導入依賴的package包/類
@Override
public Byte decode(InputStream inStream)
    throws IOException, CoderException {
  try {
    // value will be between 0-255, -1 for EOF
    int value = inStream.read();
    if (value == -1) {
      throw new EOFException("EOF encountered decoding 1 byte from input stream");
    }
    return (byte) value;
  } catch (EOFException | UTFDataFormatException exn) {
    // These exceptions correspond to decoding problems, so change
    // what kind of exception they're branded as.
    throw new CoderException(exn);
  }
}
 
開發者ID:apache,項目名稱:beam,代碼行數:17,代碼來源:ByteCoder.java


注:本文中的java.io.UTFDataFormatException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。