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


Java CodingErrorAction.REPLACE屬性代碼示例

本文整理匯總了Java中java.nio.charset.CodingErrorAction.REPLACE屬性的典型用法代碼示例。如果您正苦於以下問題:Java CodingErrorAction.REPLACE屬性的具體用法?Java CodingErrorAction.REPLACE怎麽用?Java CodingErrorAction.REPLACE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在java.nio.charset.CodingErrorAction的用法示例。


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

示例1: B2CConverter

public B2CConverter(String encoding, boolean replaceOnError)
        throws IOException {
    byte[] left = new byte[LEFTOVER_SIZE];
    leftovers = ByteBuffer.wrap(left);
    CodingErrorAction action;
    if (replaceOnError) {
        action = CodingErrorAction.REPLACE;
    } else {
        action = CodingErrorAction.REPORT;
    }
    Charset charset = getCharset(encoding);
    // Special case. Use the Apache Harmony based UTF-8 decoder because it
    // - a) rejects invalid sequences that the JVM decoder does not
    // - b) fails faster for some invalid sequences
    if (charset.equals(UTF_8)) {
        decoder = new Utf8Decoder();
    } else {
        decoder = charset.newDecoder();
    }
    decoder.onMalformedInput(action);
    decoder.onUnmappableCharacter(action);
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:22,代碼來源:B2CConverter.java

示例2: getDecoder

public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
    Charset cs = (this.charset == null)
        ? Charset.forName(encodingName)
        : this.charset;
    CharsetDecoder decoder = cs.newDecoder();

    CodingErrorAction action;
    if (ignoreEncodingErrors)
        action = CodingErrorAction.REPLACE;
    else
        action = CodingErrorAction.REPORT;

    return decoder
        .onMalformedInput(action)
        .onUnmappableCharacter(action);
}
 
開發者ID:tranleduy2000,項目名稱:javaide,代碼行數:16,代碼來源:BaseFileManager.java

示例3: B2CConverter

public B2CConverter(String encoding, boolean replaceOnError) throws IOException {
	byte[] left = new byte[LEFTOVER_SIZE];
	leftovers = ByteBuffer.wrap(left);
	CodingErrorAction action;
	if (replaceOnError) {
		action = CodingErrorAction.REPLACE;
	} else {
		action = CodingErrorAction.REPORT;
	}
	Charset charset = getCharset(encoding);
	// Special case. Use the Apache Harmony based UTF-8 decoder because it
	// - a) rejects invalid sequences that the JVM decoder does not
	// - b) fails faster for some invalid sequences
	if (charset.equals(UTF_8)) {
		decoder = new Utf8Decoder();
	} else {
		decoder = charset.newDecoder();
	}
	decoder.onMalformedInput(action);
	decoder.onUnmappableCharacter(action);
}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:21,代碼來源:B2CConverter.java

示例4: ResettableFileInputStream

/**
 *
 * @param file
 *        File to read
 *
 * @param tracker
 *        PositionTracker implementation to make offset position durable
 *
 * @param bufSize
 *        Size of the underlying buffer used for input. If lesser than {@link #MIN_BUF_SIZE},
 *        a buffer of length {@link #MIN_BUF_SIZE} will be created instead.
 *
 * @param charset
 *        Character set used for decoding text, as necessary
 *
 * @param decodeErrorPolicy
 *        A {@link DecodeErrorPolicy} instance to determine how
 *        the decoder should behave in case of malformed input and/or
 *        unmappable character.
 *
 * @throws FileNotFoundException If the file to read does not exist
 * @throws IOException If the position reported by the tracker cannot be sought
 */
public ResettableFileInputStream(File file, PositionTracker tracker,
    int bufSize, Charset charset, DecodeErrorPolicy decodeErrorPolicy)
    throws IOException {
  this.file = file;
  this.tracker = tracker;
  this.in = new FileInputStream(file);
  this.chan = in.getChannel();
  this.buf = ByteBuffer.allocateDirect(Math.max(bufSize, MIN_BUF_SIZE));
  buf.flip();
  this.byteBuf = new byte[1]; // single byte
  this.charBuf = CharBuffer.allocate(2); // two chars for surrogate pairs
  charBuf.flip();
  this.fileSize = file.length();
  this.decoder = charset.newDecoder();
  this.position = 0;
  this.syncPosition = 0;
  if (charset.name().startsWith("UTF-8")) {
    // some JDKs wrongly report 3 bytes max
    this.maxCharWidth = 4;
  } else if (charset.name().startsWith("UTF-16")) {
    // UTF_16BE and UTF_16LE wrongly report 2 bytes max
    this.maxCharWidth = 4;
  } else if (charset.name().startsWith("UTF-32")) {
    // UTF_32BE and UTF_32LE wrongly report 4 bytes max
    this.maxCharWidth = 8;
  } else {
    this.maxCharWidth = (int) Math.ceil(charset.newEncoder().maxBytesPerChar());
  }

  CodingErrorAction errorAction;
  switch (decodeErrorPolicy) {
    case FAIL:
      errorAction = CodingErrorAction.REPORT;
      break;
    case REPLACE:
      errorAction = CodingErrorAction.REPLACE;
      break;
    case IGNORE:
      errorAction = CodingErrorAction.IGNORE;
      break;
    default:
      throw new IllegalArgumentException(
          "Unexpected value for decode error policy: " + decodeErrorPolicy);
  }
  decoder.onMalformedInput(errorAction);
  decoder.onUnmappableCharacter(errorAction);

  seek(tracker.getPosition());
}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:72,代碼來源:ResettableFileInputStream.java

示例5: encode

public int encode(char[] sa, int sp, int len, byte[] da) {
    int sl = sp + len;
    int dp = 0;
    int dlASCII = dp + Math.min(len, da.length);

    // ASCII only optimized loop
    while (dp < dlASCII && sa[sp] < '\u0080')
        da[dp++] = (byte) sa[sp++];

    while (sp < sl) {
        char c = sa[sp++];
        if (c < 0x80) {
            // Have at most seven bits
            da[dp++] = (byte)c;
        } else if (c < 0x800) {
            // 2 bytes, 11 bits
            da[dp++] = (byte)(0xc0 | (c >> 6));
            da[dp++] = (byte)(0x80 | (c & 0x3f));
        } else if (Character.isSurrogate(c)) {
            if (sgp == null)
                sgp = new Surrogate.Parser();
            int uc = sgp.parse(c, sa, sp - 1, sl);
            if (uc < 0) {
                if (malformedInputAction() != CodingErrorAction.REPLACE)
                    return -1;
                da[dp++] = replacement()[0];
            } else {
                to3Bytes(da, dp, Character.highSurrogate(uc));
                dp += 3;
                to3Bytes(da, dp, Character.lowSurrogate(uc));
                dp += 3;
                sp++;  // 2 chars
            }
        } else {
            // 3 bytes, 16 bits
            to3Bytes(da, dp, c);
            dp += 3;
        }
    }
    return dp;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:41,代碼來源:CESU_8.java

示例6: encode

public int encode(char[] sa, int sp, int len, byte[] da) {
    int sl = sp + len;
    int dp = 0;
    int dlASCII = dp + Math.min(len, da.length);

    // ASCII only optimized loop
    while (dp < dlASCII && sa[sp] < '\u0080')
        da[dp++] = (byte) sa[sp++];

    while (sp < sl) {
        char c = sa[sp++];
        if (c < 0x80) {
            // Have at most seven bits
            da[dp++] = (byte)c;
        } else if (c < 0x800) {
            // 2 bytes, 11 bits
            da[dp++] = (byte)(0xc0 | (c >> 6));
            da[dp++] = (byte)(0x80 | (c & 0x3f));
        } else if (Character.isSurrogate(c)) {
            if (sgp == null)
                sgp = new Surrogate.Parser();
            int uc = sgp.parse(c, sa, sp - 1, sl);
            if (uc < 0) {
                if (malformedInputAction() != CodingErrorAction.REPLACE)
                    return -1;
                da[dp++] = repl;
            } else {
                da[dp++] = (byte)(0xf0 | ((uc >> 18)));
                da[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f));
                da[dp++] = (byte)(0x80 | ((uc >>  6) & 0x3f));
                da[dp++] = (byte)(0x80 | (uc & 0x3f));
                sp++;  // 2 chars
            }
        } else {
            // 3 bytes, 16 bits
            da[dp++] = (byte)(0xe0 | ((c >> 12)));
            da[dp++] = (byte)(0x80 | ((c >>  6) & 0x3f));
            da[dp++] = (byte)(0x80 | (c & 0x3f));
        }
    }
    return dp;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:42,代碼來源:UTF_8.java


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