Java Code Examples for java.nio.CharBuffer#array()
The following examples show how to use
java.nio.CharBuffer#array() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleByteDecoder.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { int b = sa[sp]; char c = decode(b); if (c == '\uFFFD') return CoderResult.unmappableForLength(1); if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = c; sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 2
Source File: ISO_8859_1.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int soff = src.arrayOffset(); int sp = soff + src.position(); int sl = soff + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int doff = dst.arrayOffset(); int dp = doff + dst.position(); int dl = doff + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int dlen = dl - dp; int slen = sl - sp; int len = (dlen < slen) ? dlen : slen; try { int ret = encodeISOArray(sa, sp, da, dp, len); sp = sp + ret; dp = dp + ret; if (ret != len) { if (sgp.parse(sa[sp], sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (len < slen) return CoderResult.OVERFLOW; return CoderResult.UNDERFLOW; } finally { src.position(sp - soff); dst.position(dp - doff); } }
Example 3
Source File: ISO_8859_1.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int soff = src.arrayOffset(); int sp = soff + src.position(); int sl = soff + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int doff = dst.arrayOffset(); int dp = doff + dst.position(); int dl = doff + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int dlen = dl - dp; int slen = sl - sp; int len = (dlen < slen) ? dlen : slen; try { int ret = encodeISOArray(sa, sp, da, dp, len); sp = sp + ret; dp = dp + ret; if (ret != len) { if (sgp.parse(sa[sp], sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (len < slen) return CoderResult.OVERFLOW; return CoderResult.UNDERFLOW; } finally { src.position(sp - soff); dst.position(dp - doff); } }
Example 4
Source File: Symbol.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { char c = sa[sp]; if (dl - dp < 1) return CoderResult.OVERFLOW; if (!canEncode(c)) return CoderResult.unmappableForLength(1); sp++; if (c >= 0x2200 && c <= 0x22ef){ da[dp++] = table_math[c - 0x2200]; } else if (c >= 0x0391 && c <= 0x03d6) { da[dp++]= table_greek[c - 0x0391]; } } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 5
Source File: US_ASCII.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { byte b = sa[sp]; if (b >= 0) { if (dp >= dl) return CoderResult.OVERFLOW; da[dp++] = (char)b; sp++; continue; } return CoderResult.malformedForLength(1); } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 6
Source File: X11KSC5601.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); try { while (sp < sl) { char c = sa[sp]; if (c <= '\u007f') return CoderResult.unmappableForLength(1); int ncode = encodeDouble(c); if (ncode != 0 && c != '\u0000' ) { da[dp++] = (byte) ((ncode >> 8) & 0x7f); da[dp++] = (byte) (ncode & 0x7f); sp++; continue; } return CoderResult.unmappableForLength(1); } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 7
Source File: WingDings.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { char c = sa[sp]; if (dl - dp < 1) return CoderResult.OVERFLOW; if (!canEncode(c)) return CoderResult.unmappableForLength(1); sp++; da[dp++] = table[c - 0x2700]; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 8
Source File: X11KSC5601_OLD.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { if ( sl - sp < 2) { return CoderResult.UNDERFLOW; } int b1 = sa[sp] & 0xFF | 0x80; int b2 = sa[sp + 1] & 0xFF | 0x80; char c = decodeDouble(b1, b2); if (c == replacement().charAt(0)) { return CoderResult.unmappableForLength(2); } if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = c; sp +=2; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 9
Source File: DBCS_IBM_EBCDIC_Encoder.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); int outputSize = 0; // size of output int spaceNeeded; try { while (sp < sl) { int index; int theBytes; char c = sa[sp]; if (Surrogate.is(c)) { if (sgp.parse(c, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (c >= '\uFFFE') return CoderResult.unmappableForLength(1); index = index1[((c & mask1) >> shift)] + (c & mask2); if (index < 15000) theBytes = (int)(index2.charAt(index)); else theBytes = (int)(index2a.charAt(index-15000)); b1= (byte)((theBytes & 0x0000ff00)>>8); b2 = (byte)(theBytes & 0x000000ff); if (b1 == 0x00 && b2 == 0x00 && c != '\u0000') { return CoderResult.unmappableForLength(1); } if (currentState == DBCS && b1 == 0x00) { if (dl - dp < 1) return CoderResult.OVERFLOW; currentState = SBCS; da[dp++] = SI; } else if (currentState == SBCS && b1 != 0x00) { if (dl - dp < 1) return CoderResult.OVERFLOW; currentState = DBCS; da[dp++] = SO; } if (currentState == DBCS) spaceNeeded = 2; else spaceNeeded = 1; if (dl - dp < spaceNeeded) return CoderResult.OVERFLOW; if (currentState == SBCS) da[dp++] = b2; else { da[dp++] = b1; da[dp++] = b2; } sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 10
Source File: SimpleEUCEncoder.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int index; int spaceNeeded; int i; try { while (sp < sl) { boolean allZeroes = true; char inputChar = sa[sp]; if (Character.isSurrogate(inputChar)) { if (sgp.parse(inputChar, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (inputChar >= '\uFFFE') return CoderResult.unmappableForLength(1); String theChars; char aChar; // We have a valid character, get the bytes for it index = index1[((inputChar & mask1) >> shift)] + (inputChar & mask2); if (index < 7500) theChars = index2; else if (index < 15000) { index = index - 7500; theChars = index2a; } else if (index < 22500){ index = index - 15000; theChars = index2b; } else { index = index - 22500; theChars = index2c; } aChar = theChars.charAt(2*index); outputByte[0] = (byte)((aChar & 0xff00)>>8); outputByte[1] = (byte)(aChar & 0x00ff); aChar = theChars.charAt(2*index + 1); outputByte[2] = (byte)((aChar & 0xff00)>>8); outputByte[3] = (byte)(aChar & 0x00ff); for (i = 0; i < outputByte.length; i++) { if (outputByte[i] != 0x00) { allZeroes = false; break; } } if (allZeroes && inputChar != '\u0000') { return CoderResult.unmappableForLength(1); } int oindex = 0; for (spaceNeeded = outputByte.length; spaceNeeded > 1; spaceNeeded--){ if (outputByte[oindex++] != 0x00 ) break; } if (dp + spaceNeeded > dl) return CoderResult.OVERFLOW; for (i = outputByte.length - spaceNeeded; i < outputByte.length; i++) { da[dp++] = outputByte[i]; } sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 11
Source File: ISCII91.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int outputSize = 0; try { char inputChar; while (sp < sl) { int index = Integer.MIN_VALUE; inputChar = sa[sp]; if (inputChar >= 0x0000 && inputChar <= 0x007f) { if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = (byte) inputChar; sp++; continue; } // if inputChar == ZWJ replace it with halant // if inputChar == ZWNJ replace it with Nukta if (inputChar == 0x200c) { inputChar = HALANT_CHAR; } else if (inputChar == 0x200d) { inputChar = NUKTA_CHAR; } if (inputChar >= 0x0900 && inputChar <= 0x097f) { index = ((int)(inputChar) - 0x0900)*2; } if (Character.isSurrogate(inputChar)) { if (sgp.parse(inputChar, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (index == Integer.MIN_VALUE || encoderMappingTable[index] == NO_CHAR) { return CoderResult.unmappableForLength(1); } else { if(encoderMappingTable[index + 1] == NO_CHAR) { if(dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = encoderMappingTable[index]; } else { if(dl - dp < 2) return CoderResult.OVERFLOW; da[dp++] = encoderMappingTable[index]; da[dp++] = encoderMappingTable[index + 1]; } sp++; } } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 12
Source File: DoubleByte.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
protected CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); try { while (sp < sl) { char c = sa[sp]; int bb = encodeChar(c); if (bb == UNMAPPABLE_ENCODING) { if (Character.isSurrogate(c)) { if (sgp().parse(c, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } return CoderResult.unmappableForLength(1); } if (bb > MAX_SINGLEBYTE) { // DoubleByte if (currentState == SBCS) { if (dl - dp < 1) return CoderResult.OVERFLOW; currentState = DBCS; da[dp++] = SO; } if (dl - dp < 2) return CoderResult.OVERFLOW; da[dp++] = (byte)(bb >> 8); da[dp++] = (byte)bb; } else { // SingleByte if (currentState == DBCS) { if (dl - dp < 1) return CoderResult.OVERFLOW; currentState = SBCS; da[dp++] = SI; } if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = (byte)bb; } sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 13
Source File: SimpleEUCEncoder.java From hottub with GNU General Public License v2.0 | 4 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int index; int spaceNeeded; int i; try { while (sp < sl) { boolean allZeroes = true; char inputChar = sa[sp]; if (Character.isSurrogate(inputChar)) { if (sgp.parse(inputChar, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (inputChar >= '\uFFFE') return CoderResult.unmappableForLength(1); String theChars; char aChar; // We have a valid character, get the bytes for it index = index1[((inputChar & mask1) >> shift)] + (inputChar & mask2); if (index < 7500) theChars = index2; else if (index < 15000) { index = index - 7500; theChars = index2a; } else if (index < 22500){ index = index - 15000; theChars = index2b; } else { index = index - 22500; theChars = index2c; } aChar = theChars.charAt(2*index); outputByte[0] = (byte)((aChar & 0xff00)>>8); outputByte[1] = (byte)(aChar & 0x00ff); aChar = theChars.charAt(2*index + 1); outputByte[2] = (byte)((aChar & 0xff00)>>8); outputByte[3] = (byte)(aChar & 0x00ff); for (i = 0; i < outputByte.length; i++) { if (outputByte[i] != 0x00) { allZeroes = false; break; } } if (allZeroes && inputChar != '\u0000') { return CoderResult.unmappableForLength(1); } int oindex = 0; for (spaceNeeded = outputByte.length; spaceNeeded > 1; spaceNeeded--){ if (outputByte[oindex++] != 0x00 ) break; } if (dp + spaceNeeded > dl) return CoderResult.OVERFLOW; for (i = outputByte.length - spaceNeeded; i < outputByte.length; i++) { da[dp++] = outputByte[i]; } sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 14
Source File: DoubleByteDecoder.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { int b1, b2; b1 = sa[sp]; int inputSize = 1; int outputSize = 1; highSurrogate = lowSurrogate = 0; char c = decodeSingle(b1); if (c == REPLACE_CHAR) { b1 &= 0xff; if (sl - sp < 2) return CoderResult.UNDERFLOW; b2 = sa[sp + 1] & 0xff; c = decodeDouble(b1, b2); inputSize = 2; if (c == REPLACE_CHAR) return CoderResult.unmappableForLength(inputSize); outputSize = (highSurrogate > 0) ? 2: 1; } if (dl - dp < outputSize) return CoderResult.OVERFLOW; if (outputSize == 2) { da[dp++] = highSurrogate; da[dp++] = lowSurrogate; } else { da[dp++] = c; } sp += inputSize; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 15
Source File: EUC_JP.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int outputSize = 0; byte[] outputByte; int inputSize = 0; // Size of input byte[] tmpBuf = new byte[3]; try { while (sp < sl) { outputByte = tmpBuf; char c = sa[sp]; if (Character.isSurrogate(c)) { if (sgp.parse(c, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } outputSize = encodeSingle(c, outputByte); if (outputSize == 0) { // DoubleByte int ncode = encodeDouble(c); if (ncode != UNMAPPABLE_ENCODING) { if ((ncode & 0xFF0000) == 0) { outputByte[0] = (byte) ((ncode & 0xff00) >> 8); outputByte[1] = (byte) (ncode & 0xff); outputSize = 2; } else { outputByte[0] = (byte) 0x8f; outputByte[1] = (byte) ((ncode & 0xff00) >> 8); outputByte[2] = (byte) (ncode & 0xff); outputSize = 3; } } else { return CoderResult.unmappableForLength(1); } } if (dl - dp < outputSize) return CoderResult.OVERFLOW; // Put the byte in the output buffer for (int i = 0; i < outputSize; i++) { da[dp++] = outputByte[i]; } sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 16
Source File: ISCII91.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int outputSize = 0; try { char inputChar; while (sp < sl) { int index = Integer.MIN_VALUE; inputChar = sa[sp]; if (inputChar >= 0x0000 && inputChar <= 0x007f) { if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = (byte) inputChar; sp++; continue; } // if inputChar == ZWJ replace it with halant // if inputChar == ZWNJ replace it with Nukta if (inputChar == 0x200c) { inputChar = HALANT_CHAR; } else if (inputChar == 0x200d) { inputChar = NUKTA_CHAR; } if (inputChar >= 0x0900 && inputChar <= 0x097f) { index = ((int)(inputChar) - 0x0900)*2; } if (Character.isSurrogate(inputChar)) { if (sgp.parse(inputChar, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (index == Integer.MIN_VALUE || encoderMappingTable[index] == NO_CHAR) { return CoderResult.unmappableForLength(1); } else { if(encoderMappingTable[index + 1] == NO_CHAR) { if(dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = encoderMappingTable[index]; } else { if(dl - dp < 2) return CoderResult.OVERFLOW; da[dp++] = encoderMappingTable[index]; da[dp++] = encoderMappingTable[index + 1]; } sp++; } } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 17
Source File: ProgressiveStringDecoder.java From react-native-GPay with MIT License | 4 votes |
/** * Parses data to String * If there is a partial multi-byte symbol on the edge of the String it get saved to the * reminder and added to the string on the decodeNext call. * @param data * @return */ public String decodeNext(byte[] data, int length) { byte[] decodeData; if (remainder != null) { decodeData = new byte[remainder.length + length]; System.arraycopy(remainder, 0, decodeData, 0, remainder.length); System.arraycopy(data, 0, decodeData, remainder.length, length); length += remainder.length; } else { decodeData = data; } ByteBuffer decodeBuffer = ByteBuffer.wrap(decodeData, 0, length); CharBuffer result = null; boolean decoded = false; int remainderLenght = 0; while (!decoded && (remainderLenght < 4)) { try { result = mDecoder.decode(decodeBuffer); decoded = true; } catch (CharacterCodingException e) { remainderLenght++; decodeBuffer = ByteBuffer.wrap(decodeData, 0, length - remainderLenght); } } boolean hasRemainder = decoded && remainderLenght > 0; if (hasRemainder) { remainder = new byte[remainderLenght]; System.arraycopy(decodeData, length - remainderLenght, remainder, 0, remainderLenght); } else { remainder = null; } if (!decoded) { FLog.w(ReactConstants.TAG, "failed to decode string from byte array"); return EMPTY_STRING; } else { return new String(result.array(), 0, result.length()); } }
Example 18
Source File: DoubleByte.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
protected CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); try { while (sp < sl) { char c = sa[sp]; int bb = encodeChar(c); if (bb == UNMAPPABLE_ENCODING) { if (Character.isSurrogate(c)) { if (sgp().parse(c, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } return CoderResult.unmappableForLength(1); } if (bb > MAX_SINGLEBYTE) { // DoubleByte if (currentState == SBCS) { if (dl - dp < 1) return CoderResult.OVERFLOW; currentState = DBCS; da[dp++] = SO; } if (dl - dp < 2) return CoderResult.OVERFLOW; da[dp++] = (byte)(bb >> 8); da[dp++] = (byte)bb; } else { // SingleByte if (currentState == DBCS) { if (dl - dp < 1) return CoderResult.OVERFLOW; currentState = SBCS; da[dp++] = SI; } if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = (byte)bb; } sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 19
Source File: SimpleEUCDecoder.java From hottub with GNU General Public License v2.0 | 4 votes |
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { int byte1, byte2; int inputSize = 1; char outputChar = '\uFFFD'; byte1 = sa[sp] & 0xff; if ( byte1 <= 0x9f ) { // < 0x9f has its own table (G0) if (byte1 == SS2 || byte1 == SS3 ) { // No support provided for G2/G3 at this time. return CoderResult.malformedForLength(1); } outputChar = byteToCharTable.charAt(byte1); } else if (byte1 < 0xa1 || byte1 > 0xfe) { // invalid range? return CoderResult.malformedForLength(1); } else { // (G1) if (sl - sp < 2) { return CoderResult.UNDERFLOW; } byte2 = sa[sp + 1] & 0xff; inputSize++; if ( byte2 < 0xa1 || byte2 > 0xfe) { return CoderResult.malformedForLength(2); } outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1); } if (outputChar == '\uFFFD') { return CoderResult.unmappableForLength(inputSize); } if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = outputChar; sp += inputSize; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
Example 20
Source File: X11CNS11643.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { if ( sl - sp < 2) { return CoderResult.UNDERFLOW; } byte b1 = sa[sp]; byte b2 = sa[sp + 1]; char c = replacement().charAt(0); if (table == unicodeCNS3) { char[] cc = convToSurrogate((byte)(b1 | 0x80), (byte)(b2 | 0x80), table); if (cc != null && cc[0] == '\u0000') c = cc[1]; } else { c = convToUnicode((byte)(b1 | 0x80), (byte)(b2 | 0x80), table); } if (c == replacement().charAt(0) //to keep the compatibility with b2cX11CNS11643 /*|| c == '\u0000'*/) { return CoderResult.unmappableForLength(2); } if (dl - dp < 1) return CoderResult.OVERFLOW; da[dp++] = c; sp +=2; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }