All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java.nio.charset.Charset Maven / Gradle / Ivy

Go to download

JVM AOT compiler currently generating JavaScript, C++, Haxe, with initial focus on Kotlin and games.

There is a newer version: 0.6.8
Show newest version
/*
 * Copyright 2016 Carlos Ballesteros Velasco
 *
 * Licensed 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 java.nio.charset;

import com.jtransc.ds.FastStringMap;
import com.jtransc.internal.JTranscGenericCharset;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.SortedMap;

public abstract class Charset implements Comparable {
	public static boolean isSupported(String charsetName) {
		try {
			forName(charsetName);
			return true;
		} catch (UnsupportedCharsetException e) {
			return false;
		}
	}

	native public static SortedMap availableCharsets();

	static private FastStringMap charsets = null;

	static private Charset _default;

	public static Charset forName(String charsetName) {
		charsetName = charsetName.toUpperCase();

		if (charsets == null) {
			charsets = new FastStringMap();
			charsets.set("UTF-8", new JTranscGenericCharset("UTF-8", new String[]{}) {
				@Override
				public CharsetDecoder newDecoder() {
					return new CharsetDecoder(this, 1f, 4f) {
						int c;
						boolean readC = false;

						@Override
						protected CoderResult implFlush(CharBuffer out) {
							readC = false;
							return super.implFlush(out);
						}

						@Override
						protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
							while (in.hasRemaining() && out.hasRemaining()) {
								if (!readC) {
									c = in.get() & 0xFF;
									readC = true;
								}
								switch (c >> 4) {
									case 0:
									case 1:
									case 2:
									case 3:
									case 4:
									case 5:
									case 6:
									case 7: {
										// 0xxxxxxx
										out.put((char) (c));
										readC = false;
										break;
									}
									case 12:
									case 13: {
										if (in.remaining() < 1) return CoderResult.OVERFLOW;
										// 110x xxxx   10xx xxxx
										out.put((char) (((c & 0x1F) << 6) | (in.get() & 0x3F)));
										readC = false;
										break;
									}
									case 14: {
										if (in.remaining() < 2) return CoderResult.OVERFLOW;
										// 1110 xxxx  10xx xxxx  10xx xxxx
										out.put((char) (((c & 0x0F) << 12) | ((in.get() & 0x3F) << 6) | ((in.get() & 0x3F) << 0)));
										readC = false;
										break;
									}
								}
							}
							readC = false;
							if (in.hasRemaining() && !out.hasRemaining()) {
								return CoderResult.OVERFLOW;
							} else {
								return CoderResult.UNDERFLOW;
							}
						}
					};
				}
			});
			charsets.set("ISO-8859-1", new JTranscGenericCharset("ISO-8859-1", new String[]{}));
			charsets.set("UTF-16", new JTranscGenericCharset("UTF-16", new String[]{}));
			charsets.set("UTF-16LE", new JTranscGenericCharset("UTF-16LE", new String[]{}));
			charsets.set("UTF-16BE", new JTranscGenericCharset("UTF-16BE", new String[]{}));
			charsets.set("US-ASCII", new JTranscGenericCharset("US-ASCII", new String[]{}));
			charsets.set("CP866", new JTranscSingleByteCharset("CP866", new String[] {  }, "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\t\n\u000b\u000c\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418\u0419\u041a\u041b\u041c\u041d\u041e\u041f\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427\u0428\u0429\u042a\u042b\u042c\u042d\u042e\u042f\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439\u043a\u043b\u043c\u043d\u043e\u043f\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0447\u0448\u0449\u044a\u044b\u044c\u044d\u044e\u044f\u0401\u0451\u0404\u0454\u0407\u0457\u040e\u045e°\u2219·\u221a\u2116¤\u25a0\u00a0"));
		}

		if (charsets.has(charsetName)) {
			return charsets.get(charsetName);
		} else {
			throw new UnsupportedCharsetException(charsetName);
		}
	}

	static private class JTranscSingleByteCharset extends Charset {
		private String mapping;

		protected JTranscSingleByteCharset(String canonicalName, String[] aliases, String mapping) {
			super(canonicalName, aliases);
			this.mapping = mapping;
		}

		@Override
		public boolean contains(Charset cs) {
			return false;
		}

		@Override
		public CharsetDecoder newDecoder() {
			return new CharsetDecoder(this, 1f, 4f) {
				@Override
				protected CoderResult implFlush(CharBuffer out) {
					return super.implFlush(out);
				}

				@Override
				protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
					while (in.hasRemaining() && out.hasRemaining()) {
						int c = in.get() & 0xFF;
						out.put(mapping.charAt(c));
					}
					if (in.hasRemaining() && !out.hasRemaining()) {
						return CoderResult.OVERFLOW;
					} else {
						return CoderResult.UNDERFLOW;
					}
				}
			};
		}

		@Override
		public CharsetEncoder newEncoder() {
			return null;
		}
	}

	public static Charset defaultCharset() {
		if (_default == null) _default = forName("UTF-8");
		return _default;
	}

	private String canonicalName;
	private Set aliases;

	protected Charset(String canonicalName, String[] aliases) {
		this.canonicalName = canonicalName;
		this.aliases = new HashSet();
		for (int n = 0; n < aliases.length; n++) this.aliases.add(aliases[n]);
	}

	public final String name() {
		return canonicalName;
	}

	public final Set aliases() {
		return this.aliases;
	}

	public String displayName() {
		return canonicalName;
	}

	public final boolean isRegistered() {
		return true;
	}

	public String displayName(Locale locale) {
		return canonicalName;
	}

	public abstract boolean contains(Charset cs);

	public abstract CharsetDecoder newDecoder();

	public abstract CharsetEncoder newEncoder();

	public boolean canEncode() {
		return true;
	}

	public final CharBuffer decode(ByteBuffer bb) {
		try {
			byte[] data = new byte[bb.limit()];
			for (int n = 0; n < data.length; n++) data[n] = bb.get(n);
			return CharBuffer.wrap(new String(data, canonicalName));
		} catch (UnsupportedEncodingException e) {
			throw new Error(e);
		}
	}

	public final ByteBuffer encode(CharBuffer cb) {
		try {
			char[] data = new char[cb.limit()];
			for (int n = 0; n < data.length; n++) data[n] = cb.get(n);
			return ByteBuffer.wrap(new String(data).getBytes(canonicalName));
		} catch (UnsupportedEncodingException e) {
			throw new Error(e);
		}
	}

	public final ByteBuffer encode(String str) {
		return encode(CharBuffer.wrap(str));
	}

	public final int hashCode() {
		return displayName().hashCode();
	}

	native public final int compareTo(Charset that);

	native public final boolean equals(Object ob);

	public final String toString() {
		return displayName();
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy