1 /*
2 * This class provides conversions to ASCII strings without breaking
3 * compatibility with Java 1.5.
4 */
5 package nom.tam.util;
6
7 import java.nio.charset.Charset;
8 import java.text.ParsePosition;
9
10 /*
11 * #%L
12 * nom.tam FITS library
13 * %%
14 * Copyright (C) 2004 - 2024 nom-tam-fits
15 * %%
16 * This is free and unencumbered software released into the public domain.
17 *
18 * Anyone is free to copy, modify, publish, use, compile, sell, or
19 * distribute this software, either in source code form or as a compiled
20 * binary, for any purpose, commercial or non-commercial, and by any
21 * means.
22 *
23 * In jurisdictions that recognize copyright laws, the author or authors
24 * of this software dedicate any and all copyright interest in the
25 * software to the public domain. We make this dedication for the benefit
26 * of the public at large and to the detriment of our heirs and
27 * successors. We intend this dedication to be an overt act of
28 * relinquishment in perpetuity of all present and future rights to this
29 * software under copyright law.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
35 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
37 * OTHER DEALINGS IN THE SOFTWARE.
38 * #L%
39 */
40
41 /**
42 * (<i>for internal use</i>) Various static functios to handle ASCII sequences
43 *
44 * @author tmcglynn
45 */
46 public final class AsciiFuncs {
47
48 private static final Charset US_ASCII = Charset.forName("US-ASCII");
49
50 /**
51 * utility class not to be instantiated.
52 */
53 private AsciiFuncs() {
54 }
55
56 /**
57 * Convert to ASCII or return null if not compatible.
58 *
59 * @return the String represented by the bytes
60 *
61 * @param buf the bytes representing a string
62 */
63 public static String asciiString(byte[] buf) {
64 return asciiString(buf, 0, buf.length);
65 }
66
67 /**
68 * Convert to ASCII or return null if not compatible.
69 *
70 * @param buf buffer to get the string bytes from
71 * @param start the position where the string starts
72 * @param len the length of the string
73 *
74 * @return the extracted string
75 */
76 public static String asciiString(byte[] buf, int start, int len) {
77 return new String(buf, start, len, US_ASCII);
78 }
79
80 /**
81 * Convert an ASCII string to bytes.
82 *
83 * @return the string converted to bytes
84 *
85 * @param in the string to convert
86 */
87 public static byte[] getBytes(String in) {
88 return in.getBytes(US_ASCII);
89 }
90
91 /**
92 * @deprecated Use {@link Character#isWhitespace(char)} instead.
93 *
94 * @param c the character to check
95 *
96 * @return <code>true</code> if it is a white-space character, otherwise <code>false</code>.
97 */
98 @Deprecated
99 public static boolean isWhitespace(char c) {
100 return Character.isWhitespace(c);
101 }
102
103 /**
104 * Returns an integer value contained in a string the specified position. Leading spaces will be skipped and the
105 * parsing will stop at the first non-digit character after. *
106 *
107 * @param s A string
108 * @param pos the position in the string to parse an integer. The position is updated to
109 * point to after the white spaces and integer component (if any).
110 *
111 * @return the integer value parsed.
112 *
113 * @throws NumberFormatException if there is no integer value present at the position.
114 * @throws IndexOutOfBoundsException if the parse position is outside of the string bounds
115 *
116 * @since 1.18
117 */
118 public static int parseInteger(String s, ParsePosition pos) throws IndexOutOfBoundsException, NumberFormatException {
119 int from = pos.getIndex();
120
121 for (; from < s.length(); from++) {
122 if (!Character.isWhitespace(s.charAt(from))) {
123 break;
124 }
125 }
126
127 int to = from;
128
129 if (s.charAt(from) == '-') {
130 to++;
131 }
132
133 for (; to < s.length(); to++) {
134 if (!Character.isDigit(s.charAt(to))) {
135 break;
136 }
137 }
138
139 pos.setIndex(to);
140
141 return Integer.parseInt(s.substring(from, to));
142 }
143
144 /**
145 * Returns a character from a string, incrementing the position argument.
146 *
147 * @param s a string
148 * @param pos the position of the character to return. It is incremented.
149 *
150 * @return the character at the requested position
151 *
152 * @throws IndexOutOfBoundsException if the parse position is outside of the string bounds
153 *
154 * @since 1.18
155 */
156 public static char extractChar(String s, ParsePosition pos) throws IndexOutOfBoundsException {
157 int i = pos.getIndex();
158 pos.setIndex(i + 1);
159 return s.charAt(i);
160 }
161 }