/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.tomcat.util.buf;
/** * Encodes characters as bytes using UTF-8. Extracted from Apache Harmony with some minor bug fixes applied.
*/ publicclass Utf8Encoder extends CharsetEncoder {
public Utf8Encoder() { super(StandardCharsets.UTF_8, 1.1f, 4.0f);
}
private CoderResult encodeHasArray(CharBuffer in, ByteBuffer out) { int outRemaining = out.remaining(); int pos = in.position(); int limit = in.limit(); byte[] bArr; char[] cArr; int x = pos;
bArr = out.array();
cArr = in.array(); int outPos = out.position(); int rem = in.remaining(); for (x = pos; x < pos + rem; x++) { int jchar = (cArr[x] & 0xFFFF);
// in has to have one byte more. if (limit <= x + 1) {
in.position(x);
out.position(outPos); return CoderResult.UNDERFLOW;
}
if (outRemaining < 4) {
in.position(x);
out.position(outPos); return CoderResult.OVERFLOW;
}
// The surrogate pair starts with a low-surrogate. if (jchar >= 0xDC00) {
in.position(x);
out.position(outPos); return CoderResult.malformedForLength(1);
}
int jchar2 = cArr[x + 1] & 0xFFFF;
// The surrogate pair ends with a high-surrogate. if (jchar2 < 0xDC00) {
in.position(x);
out.position(outPos); return CoderResult.malformedForLength(1);
}
// Note, the Unicode scalar value n is defined // as follows: // n = (jchar-0xD800)*0x400+(jchar2-0xDC00)+0x10000 // Where jchar is a high-surrogate, // jchar2 is a low-surrogate. int n = (jchar << 10) + jchar2 + 0xFCA02400;
// in has to have one byte more. if (limit <= pos + 1) { return CoderResult.UNDERFLOW;
}
if (outRemaining < 4) { return CoderResult.OVERFLOW;
}
// The surrogate pair starts with a low-surrogate. if (jchar >= 0xDC00) { return CoderResult.malformedForLength(1);
}
int jchar2 = (in.get() & 0xFFFF);
// The surrogate pair ends with a high-surrogate. if (jchar2 < 0xDC00) { return CoderResult.malformedForLength(1);
}
// Note, the Unicode scalar value n is defined // as follows: // n = (jchar-0xD800)*0x400+(jchar2-0xDC00)+0x10000 // Where jchar is a high-surrogate, // jchar2 is a low-surrogate. int n = (jchar << 10) + jchar2 + 0xFCA02400;
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 ist noch experimentell.