xref: /onnv-gate/usr/src/lib/libslp/javalib/com/sun/slp/Opaque.java (revision 7298:b69e27387f74)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM  * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
230Sstevel@tonic-gate  * All rights reserved.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate //  Opaque.java:   Wrapper for byte[].
280Sstevel@tonic-gate //  Author:           James Kempf
290Sstevel@tonic-gate //  Created On:       Tue Apr  7 15:21:58 1998
300Sstevel@tonic-gate //  Last Modified By: James Kempf
310Sstevel@tonic-gate //  Last Modified On: Fri Jun  5 15:26:59 1998
320Sstevel@tonic-gate //  Update Count:     38
330Sstevel@tonic-gate //
340Sstevel@tonic-gate 
350Sstevel@tonic-gate package com.sun.slp;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate import java.util.*;
380Sstevel@tonic-gate import java.io.*;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /**
410Sstevel@tonic-gate  * The Opaque class wraps Java byte arrays so we can do object-like
420Sstevel@tonic-gate  * things, such as deep equality comparison and printing.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * @author James Kempf
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate class Opaque extends Object {
480Sstevel@tonic-gate 
490Sstevel@tonic-gate     // Character to use for fill.
500Sstevel@tonic-gate 
510Sstevel@tonic-gate     private static final char ZERO = '0';
520Sstevel@tonic-gate 
530Sstevel@tonic-gate     // The byte array.
540Sstevel@tonic-gate 
550Sstevel@tonic-gate     byte[] bytes;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate     // For identifying opaques.
580Sstevel@tonic-gate 
590Sstevel@tonic-gate     final static String OPAQUE_HEADER = "\\ff";
600Sstevel@tonic-gate 
610Sstevel@tonic-gate     // Construct a Opaque.
620Sstevel@tonic-gate 
Opaque(byte[] nb)630Sstevel@tonic-gate     Opaque(byte[] nb) {
640Sstevel@tonic-gate 	bytes = nb;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate     }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate     // Construct a byte array from an escaped string.
690Sstevel@tonic-gate 
unescapeByteArray(String str)700Sstevel@tonic-gate     static byte[] unescapeByteArray(String str)
710Sstevel@tonic-gate 	throws ServiceLocationException {
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	// Check for opaque header.
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (!str.startsWith(OPAQUE_HEADER)) {
760Sstevel@tonic-gate 	    throw
770Sstevel@tonic-gate 		new ServiceLocationException(
780Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
790Sstevel@tonic-gate 				"no_opaque_header",
800Sstevel@tonic-gate 				new Object[] {str});
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	String string = str.substring(OPAQUE_HEADER.length());
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	// Process escapes to remove slash.
870Sstevel@tonic-gate 	//  string.
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	int i, n = string.length();
900Sstevel@tonic-gate 	int len = 0;
910Sstevel@tonic-gate 	int nlen = n / 3;
920Sstevel@tonic-gate 	byte[] b = new byte[nlen];
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
950Sstevel@tonic-gate 	    if (string.charAt(i) != ServiceLocationAttribute.ESCAPE) {
960Sstevel@tonic-gate 		throw
970Sstevel@tonic-gate 		    new ServiceLocationException(
980Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
990Sstevel@tonic-gate 				"escape_err",
1000Sstevel@tonic-gate 				new Object[] {str});
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	    }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	    // Get the next two characters.
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	    if (i > n - 2) {
1070Sstevel@tonic-gate 		throw
1080Sstevel@tonic-gate 		    new ServiceLocationException(
1090Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
1100Sstevel@tonic-gate 				"nonterminating_escape",
1110Sstevel@tonic-gate 				new Object[] {str});
1120Sstevel@tonic-gate 	    }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	    if (len >= nlen) {
1150Sstevel@tonic-gate 		throw
1160Sstevel@tonic-gate 		    new ServiceLocationException(
1170Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
1180Sstevel@tonic-gate 				"wrong_char_num",
1190Sstevel@tonic-gate 				new Object[] {str});
1200Sstevel@tonic-gate 	    }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	    try {
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		i++;
1250Sstevel@tonic-gate 		b[len++] = (byte)(Integer.parseInt(
1260Sstevel@tonic-gate 				string.substring(i, i+2), 16) & 0xFF);
1270Sstevel@tonic-gate 		i++;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	    } catch (NumberFormatException ex) {
1300Sstevel@tonic-gate 		throw
1310Sstevel@tonic-gate 		    new ServiceLocationException(
1320Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
1330Sstevel@tonic-gate 				"not_hex",
1340Sstevel@tonic-gate 				new Object[] {str});
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	    }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	return b;
1410Sstevel@tonic-gate     }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate     // Overrides Object.equals().
1440Sstevel@tonic-gate 
equals(Object o)1450Sstevel@tonic-gate     public boolean equals(Object o) {
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (o == this) {
1480Sstevel@tonic-gate 	    return true;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (!(o instanceof Opaque)) {
1530Sstevel@tonic-gate 	    return false;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	byte[]  cbyte = ((Opaque)o).bytes;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	// Not equal if lengths aren't.
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (cbyte.length != bytes.length) {
1620Sstevel@tonic-gate 	    return false;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	// Check inside.
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	int i;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	for (i = 0; i < cbyte.length; i++) {
1710Sstevel@tonic-gate 	    if (cbyte[i] != bytes[i]) {
1720Sstevel@tonic-gate 		return false;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	    }
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	return true;
1780Sstevel@tonic-gate     }
1790Sstevel@tonic-gate 
toString()1800Sstevel@tonic-gate     public String toString() {
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	int i, n = bytes.length;
1830Sstevel@tonic-gate 	StringBuffer buf = new StringBuffer();
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	buf.append(OPAQUE_HEADER);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
1880Sstevel@tonic-gate 	    String str = null;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	    // Convert each byte into a string, then escape. We use
1910Sstevel@tonic-gate 	    //  an 8-bit encoding, LATIN1, since escapes are two
1920Sstevel@tonic-gate 	    //  characters only.
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	    str = Integer.toHexString(((int)bytes[i] & 0xFF));
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	    buf.append(ServiceLocationAttribute.ESCAPE);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	    if (str.length() < 2) {
1990Sstevel@tonic-gate 		buf.append(ZERO);
2000Sstevel@tonic-gate 	    }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	    buf.append(str);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	return buf.toString();
2060Sstevel@tonic-gate     }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate }
209