1 // NAME
2 // $RCSfile: DefaultAsnOctetsPrintable.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 3.3 $
7 // CREATED
8 // $Date: 2006/01/17 17:43:54 $
9 // COPYRIGHT
10 // Westhawk Ltd
11 // TO DO
12 //
13
14 /*
15 * Copyright (C) 2005 - 2006 by Westhawk Ltd
16 * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
17 *
18 * Permission to use, copy, modify, and distribute this software
19 * for any purpose and without fee is hereby granted, provided
20 * that the above copyright notices appear in all copies and that
21 * both the copyright notice and this permission notice appear in
22 * supporting documentation.
23 * This software is provided "as is" without express or implied
24 * warranty.
25 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
26 */
27
28 package uk.co.westhawk.snmp.stack;
29
30 /*-
31 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32 * SNMP Java Client
33 * ჻჻჻჻჻჻
34 * Copyright 2023 MetricsHub, Westhawk
35 * ჻჻჻჻჻჻
36 * This program is free software: you can redistribute it and/or modify
37 * it under the terms of the GNU Lesser General Public License as
38 * published by the Free Software Foundation, either version 3 of the
39 * License, or (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Lesser Public License for more details.
45 *
46 * You should have received a copy of the GNU General Lesser Public
47 * License along with this program. If not, see
48 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50 */
51
52 /**
53 * Default implementation of AsnOctetsPrintableFace.
54 * This class has no effect on the way AsnOctets with type IPADDRESS
55 * or OPAQUE are printed.
56 *
57 * <p>
58 * When the type is ASN_OCTET_STR, the method tries to guess whether
59 * or not the string is printable; without the knowledge of the MIB
60 * it cannot distinguish between OctetString and any textual
61 * conventions, like DisplayString, InternationalDisplayString or DateAndTime.
62 * </p>
63 *
64 * @since 4_14
65 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
66 * @version $Revision: 3.3 $ $Date: 2006/01/17 17:43:54 $
67 */
68 public class DefaultAsnOctetsPrintable implements AsnOctetsPrintableFace {
69 static final String version_id = "@(#)$Id: DefaultAsnOctetsPrintable.java,v 3.3 2006/01/17 17:43:54 birgit Exp $ Copyright Westhawk Ltd";
70
71 public DefaultAsnOctetsPrintable() {
72 }
73
74 /**
75 * Returns whether or not the AsnOctets' byte array represent a printable
76 * string or not.
77 *
78 * <p>
79 * This method can only make a rough guess. There is no way it always
80 * gets it right.
81 * It is much better to embed MIB knowledge in your implementation, and
82 * use toCalendar() or toDisplayString(), than calling toString().
83 * </p>
84 *
85 * @see AsnOctets#toCalendar()
86 * @see AsnOctets#toDisplayString()
87 * @see AsnOctets#toHex()
88 * @see AsnOctets#toString()
89 */
90 public boolean isPrintable(byte[] value) {
91 int length = value.length;
92 int b = ' '; // the first printable char in the ASCII table
93 int e = '~'; // the last printable char in the ASCII table
94
95 /*
96 * About the test for 'value[i] == 0':
97 * (Quote from one of our customers:)
98 * I've seen cases where there are embedded nulls in a sysdescr
99 * - not always at the end either - and we need to get complete
100 * data back from the device even in this situation.
101 */
102
103 boolean isPrintable = true;
104 int i = 0;
105 while (i < length && isPrintable) {
106 isPrintable = ((value[i] >= b && value[i] <= e)
107 ||
108 Character.isWhitespace((char) value[i])
109 ||
110 value[i] == 0);
111 i++;
112 }
113
114 return isPrintable;
115 }
116
117 /**
118 * Returns the String according to the platform's default character set.
119 *
120 * @see AsnOctetsPrintableFace#toInternationalDisplayString(byte[] value)
121 */
122 public String toInternationalDisplayString(byte[] value) {
123 String str = "";
124 if (value.length > 0) {
125 // this will use the platform's default charset.
126 str = new String(value).trim();
127 }
128 return str;
129 }
130
131 }