⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userstring.java

📁 JRed is a 100% Java implementation of the IrDA infrared communications protocols. JRed lets you beam
💻 JAVA
字号:
/*
**************************************************************************
** $Header: /cvsroot/jred/jred/src/com/synchrona/jred/irlap/UserString.java,v 1.1.1.1 2000/07/05 04:41:53 mpatters Exp $
**
** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
**
** This file is part of JRed, a 100% Java implementation of the IrDA
** infrared communications protocols.
**
** This file may be distributed under the terms of the Synchrona Public
** License as defined by Synchrona, Inc. and appearing in the file
** LICENSE included in the packaging of this file. The Synchrona Public
** License is based on the Q Public License as defined by Troll Tech AS
** of Norway; it differs only in its use of the courts of Florida, USA
** rather than those of Oslo, Norway.
**************************************************************************
*/
package com.synchrona.jred.irlap;

import com.synchrona.util.Assert;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.IOException;

/**
 * <a href="http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html">
 * supported character encodings
 * </a>
 */
public class UserString {
	/**
	 * All of the IrLMP datatypes begin with a one-byte type code. The
	 * UserString datatype's code in IrLMP 1.1 is 3.
	*/
	public static final byte TYPE_CODE = (byte) 3;

	// These are the IrLMP character sets. I don't think anybody outside
	// this class should care what the flags are.
	private static final int ASCII     = 0;
	private static final int LATIN1    = 1;
	private static final int LATIN2    = 2;
	private static final int LATIN3    = 3;
	private static final int LATIN4    = 4;
	private static final int LATIN5    = 5;
	private static final int LATIN6    = 6;
	private static final int LATIN7    = 7;
	private static final int LATIN8    = 8;
	private static final int LATIN9    = 9;
	// Special handling in this class for Unicode, which IrLMP encodes
	// as 16-bit characters. Java uses UTF-8 to save bytes.
	private static final int UNICODE   = 255;

	private int    m_nCharset    = -1;   // IrLMP character set
	private String m_strEncoding = null; // Java encoding
	private String m_string      = "";   // Java-ized string

	public UserString(String strSource) {
		m_nCharset = UNICODE;
		m_string   = strSource;
	}

	public UserString(int nCharset, byte [] ayBytes, int nOffset, int nLength) throws Exception {
		Assert.fail(nOffset > 0, "Offset argument was negative.");
		Assert.fail(nLength > 0, "Length argument was negative.");
		Assert.fail(ayBytes.length >= (nOffset + nLength), "Length and offset arguments exceed array bounds.");

		boolean bObexUnicode = false;
		m_strEncoding = null;

		switch ( nCharset ) {
			case ASCII:
				m_strEncoding = "ASCII";
			break;

			case LATIN1: // fall through
			case LATIN2: // fall through
			case LATIN3: // fall through
			case LATIN4: // fall through
			case LATIN5: // fall through
			case LATIN6: // fall through
			case LATIN7: // fall through
			case LATIN8: // fall through
			case LATIN9: // fall through
				// This works because IrLMP defines the LATINn
				// values as "n" (e.g., LATIN1 = 1)
				m_strEncoding = "ISO8859_" + nCharset;
			break;

			case UNICODE:
				// Do not initialize the character set parameter,
				// as we're going to have to process the characters
				// ourselves. IrOBEX uses the UTF-2 encoding, while
				// Java uses UTF-8.
				bObexUnicode = true;
			break;

			default:
				Assert.fail(false, "Invalid character set: " + nCharset);
			break;
		}

		if ( bObexUnicode ) {
			// String is written in OBEX's Unicode encoding (UTF-2)?
			// 2-byte length + 16-bit characters + 16-bit null
			InputStreamReader in        = new InputStreamReader(new ByteArrayInputStream(ayBytes, nOffset, nLength));
			StringBuffer      strBuffer = new StringBuffer();

			// Can't check for (nChar == -1) here because if a byte value
			// of 0xFF is contained in the array, it will be sign-extended
			// to -1 by the InputStreamReader, and we'll get a false EOF.
			while ( in.ready() ) {
				int nChar = in.read();
				strBuffer.append(nChar);
			}	

			m_nCharset    = UNICODE;
			m_strEncoding = "UTF_2";
			m_string      = new String(strBuffer);
		} else {
			m_nCharset = nCharset;
			m_string   = new String(ayBytes, nOffset, nLength, m_strEncoding);
		}
	}

	/*
	 * Write an IrLMP UserString to the given array of bytes.
	public int getUserString(byte [] ayBytes, int nOffset, int nLength) throws Exception {
		char [] acStringChars = new char[(nLength * 2) + 1];

		// We fail if the offsets are negative, or if they're obviously
		// incorrect.
		Assert.fail(nOffset > 0, "Offset argument was negative.");
		Assert.fail(nLength > 0, "Length argument was negative.");
		Assert.fail(ayBytes.length <= (nOffset + nLength), "Length and offset arguments exceed array bounds.");

		// If the string's just too long to fit in the array, we
		// warn the user and truncate the string.
		int nBytes = acStringChars.length * 2;
		Assert.fail(nBytes > (nOffset + nLength), "This instance's encoding produced a byte sequence that exceeds the array bounds.");

		System.arrayCopy(ayStringBytes, 0, ayBytes, nOffset, ayBytes.length);
	}
	 */

	public String getString() {
		return m_string;
	}

	public String toString() {
		return "UserString [" + m_strEncoding + "|" + m_string + "]";
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -