📄 stringmessageelement.java
字号:
*/ @Override public synchronized byte[] getBytes(boolean copy) { byte[] cachedBytes = null; if (null != cachedGetBytes) { cachedBytes = cachedGetBytes.get(); } if (null == cachedBytes) { if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) { LOG.finer( "Creating getBytes of " + getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(this))); } String charset = type.getParameter("charset"); try { cachedBytes = data.getBytes(charset); } catch (UnsupportedEncodingException caught) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "MessageElement Data could not be generated", caught); } IllegalStateException failure = new IllegalStateException("MessageElement Data could not be generated"); failure.initCause(caught); throw failure; } cachedGetBytes = new SoftReference<byte[]>(cachedBytes); } if (!copy) { return cachedBytes; } byte[] bytesCopy = cachedBytes.clone(); return bytesCopy; } /** * {@inheritDoc} */ @Override public long getCharLength() { return data.length(); } /** * {@inheritDoc} */ @Override public synchronized char[] getChars(boolean copy) { char[] cachedChars = null; if (null != cachedGetChars) { cachedChars = cachedGetChars.get(); } if (null == cachedChars) { if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) { LOG.finer("creating cachedGetChars of " + getClass().getName() + '@' + Integer.toHexString(hashCode())); } cachedChars = new char[data.length()]; data.getChars(0, data.length(), cachedChars, 0); // if this is supposed to be a shared buffer then we can cache it. cachedGetChars = new SoftReference<char[]>(cachedChars); } if (!copy) { return cachedChars; } char[] copyChars = cachedChars.clone(); return copyChars; } /** * {@inheritDoc} */ public InputStream getStream() throws IOException { byte cachedBytes[] = null; synchronized (this) { if (null != cachedGetBytes) { cachedBytes = cachedGetBytes.get(); } } if (null != cachedBytes) { return new ByteArrayInputStream(cachedBytes); } else { String charset = type.getParameter("charset"); return new CharSequenceInputStream(data, charset); } } /** * {@inheritDoc} * * @return InputStream of the stream containing element data. * @throws IOException when there is a problem getting a reader. */ public Reader getReader() throws IOException { return new StringReader(data); } /** * {@inheritDoc} */ @Override public void sendToStream(OutputStream sendTo) throws IOException { sendTo.write(getBytes(false)); } /** * {@inheritDoc} */ @Override public void sendToWriter(Writer sendTo) throws IOException { sendTo.write(data); } /** * **/ private static class CharSequenceInputStream extends InputStream { private final CharBuffer charData; private final CharsetEncoder conversion; private boolean marked = false; private byte mark_multiByteChar[]; private int mark_position; private byte multiByteChar[]; private int position; /** * @param s the char sequence * @param encoding the charset encoding */ CharSequenceInputStream(CharSequence s, String encoding) { charData = CharBuffer.wrap(s); Charset encodingCharset = Charset.forName(encoding); conversion = encodingCharset.newEncoder(); conversion.onMalformedInput(CodingErrorAction.REPLACE); conversion.onUnmappableCharacter(CodingErrorAction.REPLACE); int maxBytes = new Float(conversion.maxBytesPerChar()).intValue(); multiByteChar = new byte[maxBytes]; position = multiByteChar.length; } /** * {@inheritDoc} */ @Override public void mark(int ignored) { charData.mark(); mark_multiByteChar = multiByteChar.clone(); mark_position = position; marked = true; } /** * {@inheritDoc} */ @Override public boolean markSupported() { return true; } /** * {@inheritDoc} */ @Override public void reset() throws IOException { if (!marked) { throw new IOException("reset() called before mark()"); } charData.reset(); multiByteChar = mark_multiByteChar.clone(); position = mark_position; } /** * {@inheritDoc} */ @Override public int read() throws IOException { // prefill the buffer while (multiByteChar.length == position) { int readsome = read(multiByteChar, 0, multiByteChar.length); if (-1 == readsome) { return -1; } position = multiByteChar.length - readsome; if ((0 != position) && (0 != readsome)) { System.arraycopy(multiByteChar, 0, multiByteChar, position, readsome); } } return (multiByteChar[position++] & 0xFF); } /** * {@inheritDoc} */ @Override public int read(byte[] buffer) throws IOException { return read(buffer, 0, buffer.length); } /** * {@inheritDoc} */ @Override public int read(byte[] buffer, int offset, int length) throws IOException { // handle partial characters; if (multiByteChar.length != position) { int copying = Math.min(length, multiByteChar.length - position); System.arraycopy(multiByteChar, position, buffer, offset, copying); position += copying; return copying; } ByteBuffer bb = ByteBuffer.wrap(buffer, offset, length); int before = bb.remaining(); CoderResult result = conversion.encode(charData, bb, true); int readin = before - bb.remaining(); if (CoderResult.UNDERFLOW == result) { if (0 == readin) { return -1; } else { return readin; } } if (CoderResult.OVERFLOW == result) { return readin; } result.throwException(); // NEVERREACHED return 0; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -