Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/writers/UTF8OutputStreamWriter.java
48527 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.xml.internal.stream.writers;2627import java.io.Writer;28import java.io.OutputStream;29import java.io.IOException;3031import com.sun.org.apache.xerces.internal.util.XMLChar;3233/**34* <p>This class is used to write a stream of chars as a stream of35* bytes using the UTF8 encoding. It assumes that the underlying36* output stream is buffered or does not need additional buffering.</p>37*38* <p>It is more efficient than using a <code>java.io.OutputStreamWriter</code>39* because it does not need to be wrapped in a40* <code>java.io.BufferedWriter</code>. Creating multiple instances41* of <code>java.io.BufferedWriter</code> has been shown to be very42* expensive in JAX-WS.</p>43*44* @author [email protected]45*/46public final class UTF8OutputStreamWriter extends Writer {4748/**49* Undelying output stream. This class assumes that this50* output stream does not need buffering.51*/52OutputStream out;5354/**55* Java represents chars that are not in the Basic Multilingual56* Plane (BMP) in UTF-16. This int stores the first code unit57* for a code point encoded in two UTF-16 code units.58*/59int lastUTF16CodePoint = 0;6061public UTF8OutputStreamWriter(OutputStream out) {62this.out = out;63}6465public String getEncoding() {66return "UTF-8";67}6869public void write(int c) throws IOException {70// Check in we are encoding at high and low surrogates71if (lastUTF16CodePoint != 0) {72final int uc =73(((lastUTF16CodePoint & 0x3ff) << 10) | (c & 0x3ff)) + 0x10000;7475if (uc < 0 || uc >= 0x200000) {76throw new IOException("Atttempting to write invalid Unicode code point '" + uc + "'");77}7879out.write(0xF0 | (uc >> 18));80out.write(0x80 | ((uc >> 12) & 0x3F));81out.write(0x80 | ((uc >> 6) & 0x3F));82out.write(0x80 | (uc & 0x3F));8384lastUTF16CodePoint = 0;85return;86}8788// Otherwise, encode char as defined in UTF-889if (c < 0x80) {90// 1 byte, 7 bits91out.write((int) c);92}93else if (c < 0x800) {94// 2 bytes, 11 bits95out.write(0xC0 | (c >> 6)); // first 596out.write(0x80 | (c & 0x3F)); // second 697}98else if (c <= '\uFFFF') {99if (!XMLChar.isHighSurrogate(c) && !XMLChar.isLowSurrogate(c)) {100// 3 bytes, 16 bits101out.write(0xE0 | (c >> 12)); // first 4102out.write(0x80 | ((c >> 6) & 0x3F)); // second 6103out.write(0x80 | (c & 0x3F)); // third 6104}105else {106lastUTF16CodePoint = c;107}108}109}110111public void write(char cbuf[]) throws IOException {112for (int i = 0; i < cbuf.length; i++) {113write(cbuf[i]);114}115}116117public void write(char cbuf[], int off, int len) throws IOException {118for (int i = 0; i < len; i++) {119write(cbuf[off + i]);120}121}122123public void write(String str) throws IOException {124final int len = str.length();125for (int i = 0; i < len; i++) {126write(str.charAt(i));127}128}129130public void write(String str, int off, int len) throws IOException {131for (int i = 0; i < len; i++) {132write(str.charAt(off + i));133}134}135136public void flush() throws IOException {137out.flush();138}139140public void close() throws IOException {141if (lastUTF16CodePoint != 0) {142throw new IllegalStateException("Attempting to close a UTF8OutputStreamWriter"143+ " while awaiting for a UTF-16 code unit");144}145out.close();146}147148}149150151