// transient as serialization is primarily for values via, e.g. JMX privatetransient ByteInputChannel in = null; privatetransient ByteOutputChannel out = null;
/** *Createsanew,uninitializedByteChunkobject.
*/ public ByteChunk() {
}
public ByteChunk(int initial) {
allocate(initial, -1);
}
/** *Adddatatothebuffer. * *@paramsrcBytesarray *@paramoffOffset *@paramlenLength * *@throwsIOExceptionWritingoverflowdatatotheoutputchannelfailed
*/ publicvoid append(byte src[], int off, int len) throws IOException { // will grow, up to limit
makeSpace(len); int limit = getLimitInternal();
// Optimize on a common case. // If the buffer is empty and the source is going to fill up all the // space in buffer, may as well write it directly to the output, // and avoid an extra copy if (len == limit && end == start && out != null) {
out.realWriteBytes(src, off, len); return;
}
// if we are below the limit if (len <= limit - end) {
System.arraycopy(src, off, buff, end, len);
end += len; return;
}
// Need more space than we can afford, need to flush buffer.
// The buffer is already at (or bigger than) limit.
// We chunk the data into slices fitting in the buffer limit, although // if the data is written directly if it doesn't fit.
int avail = limit - end;
System.arraycopy(src, off, buff, end, avail);
end += avail;
/** *Adddatatothebuffer. * *@paramfromtheByteBufferwiththedata * *@throwsIOExceptionWritingoverflowdatatotheoutputchannelfailed
*/ publicvoid append(ByteBuffer from) throws IOException { int len = from.remaining();
// will grow, up to limit
makeSpace(len); int limit = getLimitInternal();
// Optimize on a common case. // If the buffer is empty and the source is going to fill up all the // space in buffer, may as well write it directly to the output, // and avoid an extra copy if (len == limit && end == start && out != null) {
out.realWriteBytes(from);
from.position(from.limit()); return;
} // if we have limit and we're below if (len <= limit - end) { // makeSpace will grow the buffer to the limit, // so we have space
from.get(buff, end, len);
end += len; return;
}
// need more space than we can afford, need to flush // buffer
// the buffer is already at ( or bigger than ) limit
// We chunk the data into slices fitting in the buffer limit, although // if the data is written directly if it doesn't fit
int avail = limit - end;
from.get(buff, end, avail);
end += avail;
flushBuffer();
int fromLimit = from.limit(); int remain = len - avail;
avail = limit - end; while (remain >= avail) {
from.limit(from.position() + avail);
out.realWriteBytes(from);
from.position(from.limit());
remain = remain - avail;
}
from.limit(fromLimit);
from.get(buff, end, remain);
end += remain;
}
// -------------------- Removing data from the buffer --------------------
// Compacts buffer
System.arraycopy(buff, start, tmp, 0, end - start);
buff = tmp;
tmp = null;
end = end - start;
start = 0;
}
// -------------------- Conversion and getters --------------------
@Override public String toString() { try { return toString(CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
} catch (CharacterCodingException e) { // Unreachable code. Use of REPLACE above means the exception will never be thrown. thrownew IllegalStateException(e);
}
}
/** *ConvertsthecurrentcontentofthebytebuffertoaStringusingtheconfiguredcharacterset. * *@returnTheresultofconvertingthebytestoaString * *@deprecatedUnused.ThismethodwillberemovedinTomcat11onwards.
*/
@Deprecated public String toStringInternal() { try { return toStringInternal(CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
} catch (CharacterCodingException e) { // Unreachable code. Use of REPLACE above means the exception will never be thrown. thrownew IllegalStateException(e);
}
}
/** *ConvertsthecurrentcontentofthebytebuffertoaStringusingtheconfiguredcharacterset. * *@parammalformedInputActionActiontotakeiftheinputismalformed *@paramunmappableCharacterActionActiontotakeifabytesequencecan'tbemappedtoacharacter * *@returnTheresultofconvertingthebytestoaString * *@throwsCharacterCodingExceptionIfanerroroccursduringtheconversion
*/ public String toStringInternal(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) throws CharacterCodingException { if (charset == null) {
charset = DEFAULT_CHARSET;
} // new String(byte[], int, int, Charset) takes a defensive copy of the // entire byte array. This is expensive if only a small subset of the // bytes will be used. The code below is from Apache Harmony.
CharBuffer cb; if (malformedInputAction == CodingErrorAction.REPLACE && unmappableCharacterAction == CodingErrorAction.REPLACE) {
cb = charset.decode(ByteBuffer.wrap(buff, start, end - start));
} else {
cb = charset.newDecoder().onMalformedInput(malformedInputAction)
.onUnmappableCharacter(unmappableCharacterAction).decode(ByteBuffer.wrap(buff, start, end - start));
} returnnew String(cb.array(), cb.arrayOffset(), cb.length());
}
publiclong getLong() { return Ascii.parseLong(buff, start, end - start);
}
/** *ComparesthemessagebytestothespecifiedStringobject. *<p> *NOTE:Thisonlyworksforcharactersintherange0-127. * *@paramstheStringtocompare * *@return<code>true</code>ifthecomparisonsucceeded,<code>false</code>otherwise
*/ publicboolean equals(String s) { byte[] b = buff; int len = end - start; if (b == null || len != s.length()) { returnfalse;
} int off = start; for (int i = 0; i < len; i++) { if (b[off++] != s.charAt(i)) { returnfalse;
}
} returntrue;
}
/** *ComparesthemessagebytestothespecifiedStringobject. *<p> *NOTE:Thisonlyworksforcharactersintherange0-127. * *@paramstheStringtocompare * *@return<code>true</code>ifthecomparisonsucceeded,<code>false</code>otherwise
*/ publicboolean equalsIgnoreCase(String s) { byte[] b = buff; int len = end - start; if (b == null || len != s.length()) { returnfalse;
} int off = start; for (int i = 0; i < len; i++) { if (Ascii.toLower(b[off++]) != Ascii.toLower(s.charAt(i))) { returnfalse;
}
} returntrue;
}
if (b1 == null || c2 == null || end - start != len2) { returnfalse;
} int off1 = start; int len = end - start;
while (len-- > 0) { if ((char) b1[off1++] != c2[off2++]) { returnfalse;
}
} returntrue;
}
/** *Returnstrueifthebufferstartswiththespecifiedstringwhentestedinacasesensitivemanner. *<p> *NOTE:Thisonlyworksforcharactersintherange0-127. * *@paramsthestring *@paramposTheposition * *@return<code>true</code>ifthestartmatches
*/ publicboolean startsWith(String s, int pos) { byte[] b = buff; int len = s.length(); if (b == null || len + pos > end - start) { returnfalse;
} int off = start + pos; for (int i = 0; i < len; i++) { if (b[off++] != s.charAt(i)) { returnfalse;
}
} returntrue;
}
/** *Returnstrueifthebufferstartswiththespecifiedstringwhentestedinacaseinsensitivemanner. *<p> *NOTE:Thisonlyworksforcharactersintherange0-127. * *@paramsthestring *@paramposTheposition * *@return<code>true</code>ifthestartmatches
*/ publicboolean startsWithIgnoreCase(String s, int pos) { byte[] b = buff; int len = s.length(); if (b == null || len + pos > end - start) { returnfalse;
} int off = start + pos; for (int i = 0; i < len; i++) { if (Ascii.toLower(b[off++]) != Ascii.toLower(s.charAt(i))) { returnfalse;
}
} returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.