src/share/classes/java/lang/StringCoding.java

Print this page




 205         // (3)getClass().getClassLoader0() is expensive
 206         // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
 207         // is only chcked (and then isTrusted gets set) when (SM==null). It is
 208         // possible that the SM==null for now but then SM is NOT null later
 209         // when safeTrim() is invoked...the "safe" way to do is to redundant
 210         // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
 211         // but it then can be argued that the SM is null when the opertaion
 212         // is started...
 213         CharsetDecoder cd = cs.newDecoder();
 214         int en = scale(len, cd.maxCharsPerByte());
 215         char[] ca = new char[en];
 216         if (len == 0)
 217             return ca;
 218         boolean isTrusted = false;
 219         if (System.getSecurityManager() != null) {
 220             if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
 221                 ba =  Arrays.copyOfRange(ba, off, off + len);
 222                 off = 0;
 223             }
 224         }



 225         if (cd instanceof ArrayDecoder) {
 226             int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
 227             return safeTrim(ca, clen, cs, isTrusted);
 228         } else {
 229             cd.onMalformedInput(CodingErrorAction.REPLACE)
 230               .onUnmappableCharacter(CodingErrorAction.REPLACE)
 231               .reset();
 232             ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
 233             CharBuffer cb = CharBuffer.wrap(ca);
 234             try {
 235                 CoderResult cr = cd.decode(bb, cb, true);
 236                 if (!cr.isUnderflow())
 237                     cr.throwException();
 238                 cr = cd.flush(cb);
 239                 if (!cr.isUnderflow())
 240                     cr.throwException();
 241             } catch (CharacterCodingException x) {
 242                 // Substitution is always enabled,
 243                 // so this shouldn't happen
 244                 throw new Error(x);
 245             }
 246             return safeTrim(ca, cb.position(), cs, isTrusted);
 247         }
 248     }
 249 
 250     static char[] decode(byte[] ba, int off, int len) {
 251         String csn = Charset.defaultCharset().name();


 339             if (se == null)
 340                 throw new UnsupportedEncodingException (csn);
 341             set(encoder, se);
 342         }
 343         return se.encode(ca, off, len);
 344     }
 345 
 346     static byte[] encode(Charset cs, char[] ca, int off, int len) {
 347         CharsetEncoder ce = cs.newEncoder();
 348         int en = scale(len, ce.maxBytesPerChar());
 349         byte[] ba = new byte[en];
 350         if (len == 0)
 351             return ba;
 352         boolean isTrusted = false;
 353         if (System.getSecurityManager() != null) {
 354             if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
 355                 ca =  Arrays.copyOfRange(ca, off, off + len);
 356                 off = 0;
 357             }
 358         }



 359         if (ce instanceof ArrayEncoder) {
 360             int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
 361             return safeTrim(ba, blen, cs, isTrusted);
 362         } else {
 363             ce.onMalformedInput(CodingErrorAction.REPLACE)
 364               .onUnmappableCharacter(CodingErrorAction.REPLACE)
 365               .reset();
 366             ByteBuffer bb = ByteBuffer.wrap(ba);
 367             CharBuffer cb = CharBuffer.wrap(ca, off, len);
 368             try {
 369                 CoderResult cr = ce.encode(cb, bb, true);
 370                 if (!cr.isUnderflow())
 371                     cr.throwException();
 372                 cr = ce.flush(bb);
 373                 if (!cr.isUnderflow())
 374                     cr.throwException();
 375             } catch (CharacterCodingException x) {
 376                 throw new Error(x);
 377             }
 378             return safeTrim(ba, bb.position(), cs, isTrusted);
 379         }
 380     }
 381 
 382     static byte[] encode(char[] ca, int off, int len) {
 383         String csn = Charset.defaultCharset().name();
 384         try {
 385             return encode(csn, ca, off, len);


 205         // (3)getClass().getClassLoader0() is expensive
 206         // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
 207         // is only chcked (and then isTrusted gets set) when (SM==null). It is
 208         // possible that the SM==null for now but then SM is NOT null later
 209         // when safeTrim() is invoked...the "safe" way to do is to redundant
 210         // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
 211         // but it then can be argued that the SM is null when the opertaion
 212         // is started...
 213         CharsetDecoder cd = cs.newDecoder();
 214         int en = scale(len, cd.maxCharsPerByte());
 215         char[] ca = new char[en];
 216         if (len == 0)
 217             return ca;
 218         boolean isTrusted = false;
 219         if (System.getSecurityManager() != null) {
 220             if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
 221                 ba =  Arrays.copyOfRange(ba, off, off + len);
 222                 off = 0;
 223             }
 224         }
 225         cd.onMalformedInput(CodingErrorAction.REPLACE)
 226           .onUnmappableCharacter(CodingErrorAction.REPLACE)
 227           .reset();
 228         if (cd instanceof ArrayDecoder) {
 229             int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
 230             return safeTrim(ca, clen, cs, isTrusted);
 231         } else {



 232             ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
 233             CharBuffer cb = CharBuffer.wrap(ca);
 234             try {
 235                 CoderResult cr = cd.decode(bb, cb, true);
 236                 if (!cr.isUnderflow())
 237                     cr.throwException();
 238                 cr = cd.flush(cb);
 239                 if (!cr.isUnderflow())
 240                     cr.throwException();
 241             } catch (CharacterCodingException x) {
 242                 // Substitution is always enabled,
 243                 // so this shouldn't happen
 244                 throw new Error(x);
 245             }
 246             return safeTrim(ca, cb.position(), cs, isTrusted);
 247         }
 248     }
 249 
 250     static char[] decode(byte[] ba, int off, int len) {
 251         String csn = Charset.defaultCharset().name();


 339             if (se == null)
 340                 throw new UnsupportedEncodingException (csn);
 341             set(encoder, se);
 342         }
 343         return se.encode(ca, off, len);
 344     }
 345 
 346     static byte[] encode(Charset cs, char[] ca, int off, int len) {
 347         CharsetEncoder ce = cs.newEncoder();
 348         int en = scale(len, ce.maxBytesPerChar());
 349         byte[] ba = new byte[en];
 350         if (len == 0)
 351             return ba;
 352         boolean isTrusted = false;
 353         if (System.getSecurityManager() != null) {
 354             if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
 355                 ca =  Arrays.copyOfRange(ca, off, off + len);
 356                 off = 0;
 357             }
 358         }
 359         ce.onMalformedInput(CodingErrorAction.REPLACE)
 360           .onUnmappableCharacter(CodingErrorAction.REPLACE)
 361           .reset();
 362         if (ce instanceof ArrayEncoder) {
 363             int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
 364             return safeTrim(ba, blen, cs, isTrusted);
 365         } else {



 366             ByteBuffer bb = ByteBuffer.wrap(ba);
 367             CharBuffer cb = CharBuffer.wrap(ca, off, len);
 368             try {
 369                 CoderResult cr = ce.encode(cb, bb, true);
 370                 if (!cr.isUnderflow())
 371                     cr.throwException();
 372                 cr = ce.flush(bb);
 373                 if (!cr.isUnderflow())
 374                     cr.throwException();
 375             } catch (CharacterCodingException x) {
 376                 throw new Error(x);
 377             }
 378             return safeTrim(ba, bb.position(), cs, isTrusted);
 379         }
 380     }
 381 
 382     static byte[] encode(char[] ca, int off, int len) {
 383         String csn = Charset.defaultCharset().name();
 384         try {
 385             return encode(csn, ca, off, len);