Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/print/AttributeClass.java
32287 views
/*1* Copyright (c) 2003, 2013, 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*/24package sun.print;2526import java.util.Objects;27import java.io.ByteArrayInputStream;2829public class AttributeClass {30private String myName;31private int myType;32private int nameLen;33private Object myValue;3435public static final int TAG_UNSUPPORTED_VALUE = 0x10;36public static final int TAG_INT = 0x21;37public static final int TAG_BOOL = 0x22;38public static final int TAG_ENUM = 0x23;39public static final int TAG_OCTET = 0x30;40public static final int TAG_DATE = 0x31;41public static final int TAG_RESOLUTION = 0x32;42public static final int TAG_RANGE_INTEGER = 0x33;4344public static final int TAG_TEXT_LANGUAGE = 0x35;45public static final int TAG_NAME_LANGUAGE = 0x36;4647public static final int TAG_TEXT_WO_LANGUAGE = 0x41;48public static final int TAG_NAME_WO_LANGUAGE = 0x42;49public static final int TAG_KEYWORD = 0x44;50public static final int TAG_URI = 0x45;51public static final int TAG_CHARSET = 0x47;52public static final int TAG_NATURALLANGUAGE = 0x48;53public static final int TAG_MIME_MEDIATYPE = 0x49;54public static final int TAG_MEMBER_ATTRNAME = 0x4A;555657public static final AttributeClass ATTRIBUTES_CHARSET =58new AttributeClass("attributes-charset",59TAG_CHARSET, "utf-8");60public static final AttributeClass ATTRIBUTES_NATURAL_LANGUAGE =61new AttributeClass("attributes-natural-language",62TAG_NATURALLANGUAGE, "en");6364/*65* value passed in by IPPPrintService.readIPPResponse is a sequence66* of bytes with this format67* | length1 | byte1 | byte 2 | ... byten | length2 | byte1 ... byten |68* :69* | lengthN | byte1 ... byten | total number of values|70*/71protected AttributeClass(String name, int type, Object value) {72myName = name;73myType = type;74nameLen = name.length();75myValue = value;76}7778public byte getType() {79return (byte)myType;80}8182public char[] getLenChars() {83char[] chars = new char[2];84chars[0] = 0;85chars[1] = (char)nameLen;86return chars;87}8889/**90* Returns raw data.91*/92public Object getObjectValue() {93return myValue;94}9596/**97* Returns single int value.98*/99public int getIntValue() {100byte[] bufArray = (byte[])myValue;101102if (bufArray != null) {103byte[] buf = new byte[4];104for (int i=0; i<4; i++) {105buf[i] = bufArray[i+1];106}107108return convertToInt(buf);109}110return 0;111}112113/**114* Returns array of int values.115*/116public int[] getArrayOfIntValues() {117118byte[] bufArray = (byte[])myValue;119if (bufArray != null) {120121//ArrayList valList = new ArrayList();122ByteArrayInputStream bufStream =123new ByteArrayInputStream(bufArray);124int available = bufStream.available();125126// total number of values is at the end of the stream127bufStream.mark(available);128bufStream.skip(available-1);129int length = bufStream.read();130bufStream.reset();131132int[] valueArray = new int[length];133for (int i = 0; i < length; i++) {134// read length135int valLength = bufStream.read();136if (valLength != 4) {137// invalid data138return null;139}140141byte[] bufBytes = new byte[valLength];142bufStream.read(bufBytes, 0, valLength);143valueArray[i] = convertToInt(bufBytes);144145}146return valueArray;147}148return null;149}150151/**152* Returns 2 int values.153*/154public int[] getIntRangeValue() {155int[] range = {0, 0};156byte[] bufArray = (byte[])myValue;157if (bufArray != null) {158int nBytes = 4; // 32-bit signed integer159for (int j=0; j<2; j++) { // 2 set of integers160byte[] intBytes = new byte[nBytes];161// REMIND: # bytes should be 8162for (int i=0; i< nBytes; i++) {163//+ 1 because the 1st byte is length164intBytes[i] = bufArray[i+(4*j)+1];165}166range[j] = convertToInt(intBytes);167}168}169return range;170171}172173/**174* Returns String value.175*/176public String getStringValue() {177//assumes only 1 attribute value. Will get the first value178// if > 1.179String strVal = null;180byte[] bufArray = (byte[])myValue;181if (bufArray != null) {182ByteArrayInputStream bufStream =183new ByteArrayInputStream(bufArray);184185int valLength = bufStream.read();186187byte[] strBytes = new byte[valLength];188bufStream.read(strBytes, 0, valLength);189try {190strVal = new String(strBytes, "UTF-8");191} catch (java.io.UnsupportedEncodingException uee) {192}193}194return strVal;195}196197198/**199* Returns array of String values.200*/201public String[] getArrayOfStringValues() {202203byte[] bufArray = (byte[])myValue;204if (bufArray != null) {205ByteArrayInputStream bufStream =206new ByteArrayInputStream(bufArray);207int available = bufStream.available();208209// total number of values is at the end of the stream210bufStream.mark(available);211bufStream.skip(available-1);212int length = bufStream.read();213bufStream.reset();214215String[] valueArray = new String[length];216for (int i = 0; i < length; i++) {217// read length218int valLength = bufStream.read();219byte[] bufBytes = new byte[valLength];220bufStream.read(bufBytes, 0, valLength);221try {222valueArray[i] = new String(bufBytes, "UTF-8");223} catch (java.io.UnsupportedEncodingException uee) {224}225}226return valueArray;227}228return null;229}230231232/**233* Returns single byte value.234*/235public byte getByteValue() {236byte[] bufArray = (byte[])myValue;237238if ((bufArray != null) && (bufArray.length>=2)) {239return bufArray[1];240}241return 0;242}243244/**245* Returns attribute name.246*/247public String getName() {248return myName;249}250251@Override252public boolean equals(Object obj) {253if (!(obj instanceof AttributeClass)) {254return false;255}256if (this == obj) {257return true;258}259260AttributeClass acObj = (AttributeClass) obj;261return myType == acObj.getType() &&262Objects.equals(myName, acObj.getName()) &&263Objects.equals(myValue, acObj.getObjectValue());264}265266@Override267public int hashCode() {268return Objects.hash(myType, myName, myValue);269}270271public String toString() {272return myName;273}274275private int unsignedByteToInt(byte b) {276return (int) (b & 0xff);277}278279private int convertToInt(byte[] buf) {280int intVal = 0;281int pos = 0;282intVal+= unsignedByteToInt(buf[pos++]) << 24;283intVal+= unsignedByteToInt(buf[pos++]) << 16;284intVal+= unsignedByteToInt(buf[pos++]) << 8;285intVal+= unsignedByteToInt(buf[pos++]) << 0;286return intVal;287}288}289290291