File: ValidationListTranslatedData.java

package info (click to toggle)
libjt400-java 9.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 35,452 kB
  • sloc: java: 383,267; xml: 4,278; makefile: 14
file content (410 lines) | stat: -rw-r--r-- 13,749 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
///////////////////////////////////////////////////////////////////////////////
//                                                                             
// JTOpen (IBM Toolbox for Java - OSS version)                              
//                                                                             
// Filename: ValidationListTranslatedData.java
//                                                                             
// The source code contained herein is licensed under the IBM Public License   
// Version 1.0, which has been approved by the Open Source Initiative.         
// Copyright (C) 2001-2010 International Business Machines Corporation and     
// others. All rights reserved.                                                
//                                                                             
///////////////////////////////////////////////////////////////////////////////

package com.ibm.as400.access;

/**
 * Represents language-specific information
 * that is assigned in a standardized format to a validation list entry.
 * This could be information in the non-encrypted data, encrypted data, or
 * identifier portion of a validation list entry. Maximum lengths for each
 * are 1000, 600, and 100 bytes, respectively.
 *
 * @author Thomas Johnson (tom.johnson@kingland.com), Kingland Systems Corporation
 */
public class ValidationListTranslatedData {

	private byte[] bytes_ = null;
	private int ccsid_ = 0;
/**
 * Constructs a ValidationListTranslatedData.
 */
public ValidationListTranslatedData() {
	super();
}
/**
 * Constructs a ValidationListTranslatedData from a structure stored as IBM i bytes.
 * <p>
 * The <i>offset</i> indicates the starting position of the structure in the
 * given <i>buffer</i>.
 *
 * @param buffer byte[]
 * @param offset int
 */
public ValidationListTranslatedData(byte[] buffer, int offset) {
	this();

	int dataLength =(new AS400Bin4().toInt(buffer, offset + getReadOffsetTByteLength()));
	setCcsid(new AS400Bin4().toInt(buffer, offset + getReadOffsetCcsid()));
	setBytes((byte[])(
		new AS400ByteArray(dataLength).toObject(
			buffer, offset + getReadOffsetTBytes())));
}
/**
 * Constructs a ValidationListTranslatedData from the specified IBM i <i>bytes</i>
 * which are encoded in the given <i>ccsid</i>.
 *
 * @param ccsid int
 * @param bytes byte[]
 */
public ValidationListTranslatedData(int ccsid, byte[] bytes) {
	this();
	setCcsid(ccsid);
	setBytes(bytes);
}
/**
 * Constructs a ValidationListTranslatedData from the given string.
 * <p>
 * The translated bytes are derived by converting the string to IBM i bytes
 * using the given <i>ccsid</i>. The <i>as400</i> is required to perform the
 * conversion from text to bytes. A ccsid of 0 indicates to use the ccsid
 * of the current user.
 *
 * @param s java.lang.String
 * @param ccsid int
 * @param as400 com.ibm.as400.access.AS400
 */
public ValidationListTranslatedData(String s, int ccsid, AS400 as400) {
	this();
	setBytes(s, ccsid, as400);
}
/**
 * Returns the total length of the corresponding structure when this object is
 * written to IBM i bytes for use by the validation list APIs.
 * <p>
 * This is the length of the entire structure, not just the translated bytes.
 *
 * @return int
 */
public int getByteLength() {
	// Assumes variable length data (the translated bytes) will always occur at end
	int total = getWriteOffsetTBytes();
	if (getBytes() != null)
		total += getBytes().length;
	return total;
}
/**
 * Returns the IBM i bytes comprising the translated data.
 * <p>
 * For text conversion, the bytes will be interpreted using the assigned ccsid.
 *
 * @return byte[]
 */
public byte[] getBytes() {
	return bytes_;
}
/**
 * Returns the coded character set identifier used to encode the translated bytes.
 * <p>
 * Valid CCSID values are in the range 1 through 65535. The special value 0
 * can be used to indicate the default CCSID for the current user (when
 * the validation list APIs are invoked on the system). In some cases,
 * primarily attribute data, the special value -1 is also allowed. This
 * indicates that no CCSID value is stored with the data (i.e. binary data,
 * where no conversion is required).
 *
 * @return int
 */
public int getCcsid() {
	return ccsid_;
}
/**
 * Returns the offset of CCSID information in the structure when the receiver is
 * read from IBM i bytes.
 * @return int
 */
protected int getReadOffsetCcsid() {
	return 4;
}
/**
 * Returns the offset of the length of the translated bytes when the receiver
 * is read from a IBM i byte structure.
 * @return int
 */
protected int getReadOffsetTByteLength() {
	return 0;
}
/**
 * Returns the offset of the translated bytes when the receiver is read from an
 * IBM i byte structure.
 * @return int
 */
protected int getReadOffsetTBytes() {
	return 8;
}
/**
 * Returns the result of converting the assigned IBM i bytes to a Java String
 * using the assigned CCSID. Returns null if the assigned ccsid is -1, since
 * the bytes do not represent text.
 * <p>
 * The <i>as400</i> is required to perform the conversion.
 *
 * @param as400 com.ibm.as400.access.AS400
 * @return java.lang.String
*/
public String getString(AS400 as400) {
	int ccsid = getCcsid();

	// check null or non-text value
	if (bytes_ == null || ccsid == -1)
		return null;
	// check unicode; no conversion required
	if (ccsid == 13488)
		return new String(bytes_);
	// check for default ccsid for current user id
	if (ccsid == 0)
		ccsid = as400.getCcsid();

	return
		(String)new AS400Text(bytes_.length, ccsid, as400).toObject(bytes_);
}
/**
 * Returns the length to be specified in the written IBM i byte structure
 * if the assigned data is null.
 * <p>
 * Typically this value is set to 0. However, there are some cases where
 * other values must be specified to maintain proper behavior. For
 * example, when changing an entry a structure must be specified for the
 * data to encrypt, even if the encrypted data should not be changed.
 * However, if the data length in the structure is set to 0 instead of -1,
 * the existing encrypted data is wiped out. This is undesirable since
 * we don't always want the encrypted data changed. We might want to
 * modify the unencrypted data (i.e. user statistics) while leaving
 * the encrypted data (i.e. user password) unchanged.
 *
 * @return int
 */
protected int getWriteNullDataLength() {
	return 0;
}
/**
 * Returns the offset of CCSID information in the structure when the receiver
 * is written to IBM i bytes.
 * @return int
 */
protected int getWriteOffsetCcsid() {
	return getReadOffsetCcsid();
}
/**
 * Returns the offset of the length of the translated bytes when the receiver
 * is written to an IBM i byte structure.
 * @return int
 */
protected int getWriteOffsetTByteLength() {
	return getReadOffsetTByteLength();
}
/**
 * Returns the offset of the translated bytes when the receiver is written to an
 * IBM i byte structure.
 * @return int
 */
protected int getWriteOffsetTBytes() {
	return getReadOffsetTBytes();
}
/**
 * Indicates whether the given CCSID is valid for tagging IBM i data.
 * @return true if valid; false if not.
 */
protected boolean isValidCcsid(int ccsid) {
	boolean isValid = false;
	switch(ccsid) {
		// Universal Character Set (UCS-2 and UTF-8)
		case 13488:	//  UCS-2 Level 1
		
		// CCSIDs for EBCDIC Group 1 (Latin-1) Countries
		case 37:	// USA, Canada (S/370), Netherlands, Portugal, Brazil, Australia, New Zealand
		case 256:	// Word Processing, Netherlands
		case 273:	// Austria, Germany
		case 277:	// Denmark, Norway
		case 278:	// Finland, Sweden
		case 280:	// Italy
		case 284:	// Spain, Latin America (Spanish)
		case 285:	// United Kingdom
		case 297:	// France
		case 500:	// Belgium, Canada (AS/400), Switzerland, International Latin-1
		case 871:	// Iceland
		case 924:	// Latin-0
		case 1140:	// USA, Canada (S/370), Netherlands, Portugal, Brazil, Australia, New Zealand
		case 1141:	// Austria, Germany
		case 1142:	// Denmark, Norway
		case 1143:	// Finland, Sweden
		case 1144:	// Italy
		case 1145:	// Spain, Latin America (Spanish)
		case 1146:	// United Kingdom
		case 1147:	// France
		case 1148:	// Belgium, Canada (AS/400), Switzerland, International Latin-1
		case 1149:	// Iceland
		
		// CCSIDs for EBCDIC Group 1a (Non-Latin-1 SBCS) Countries
		case 420:	// Arabic (Type 4)Visual LTR
		case 423:	// Greek
		case 424:	// Hebrew(Type 4)
		case 870:	// Latin-2 Multilingual
		case 875:	// Greek
		case 880:	// Cyrillic Multilingual
		case 905:	// Turkey Latin-3 Multilingual
		case 918:	// Urdu
		case 1025:	// Cyrillic Multilingual
		case 1026:	// Turkey Latin-5
		case 1097:	// Farsi
		case 1112:	// Baltic Multilingual
		case 1122:	// Estonia
		case 1123:	// Ukraine
		case 8612:	// Arabic (Type 5)
		case 8616:	// Hebrew (Type 6)
		case 12708:	// Arabic (Type 7)
		case 62211:	// Hebrew (Type 5)
		case 62224:	// Arabic (Type 6)
		case 62235:	// Hebrew (Type 10)
 
		// SBCS CCSIDs for EBCDIC Group 2 (DBCS) Countries
		case 290:	// Japan Katakana (extended)
		case 833:	// Korea (extended)
		case 836:	// Simplified Chinese (extended)
		case 838:	// Thailand (extended)
		case 1027:	// Japan English (extended)
		case 1130:	// Vietnam
		case 1132:	// Lao
		case 9030:	// Thailand (extended)
		case 13121:	// Korea Windows
		case 13124:	// Traditional Chinese
		case 28709:	// Traditional Chinese (extended)

		// DBCS CCSIDs for EBCDIC Group 2 (DBCS) Countries
		case 300:	// Japan - including 4370 user-defined characters (UDC)
		case 834:	// Korea - including 1880 UDC 
		case 835:	// Traditional Chinese - including 6204 UDC
		case 837:	// Simplified Chinese - including 1880 UDC
		case 4396:	// Japan - including 1880 UDC
		case 4930:	// Korea Windows
		case 4933:	// Simplified Chinese

		// Mixed CCSIDs for EBCDIC Group 2 (DBCS) Countries
		case 930:	// Japan Katakana/Kanji (extended) - including 4370 UDC
		case 933:	// Korea (extended) - including 1880 UDC
		case 935:	// Simplified Chinese (extended) - including 1880 UDC
		case 937:	// Traditional Chinese (extended) - including 4370 UDC
		case 939:	// Japan English/Kanji (extended) - including 4370 UDC
		case 1364:	// Korea (extended)
		case 1388:	// Traditional Chinese
		case 5026:	// Japan Katakana/Kanji (extended) - including 1880 UDC)
		case 5035:	// Japan English/Kanji (extended) - including 1880 UDC
			isValid = true;
	}
	return isValid;
}
/**
 * Sets the IBM i bytes comprising the translated data.
 * <p>
 * For text conversion, the bytes will be interpreted using the assigned ccsid.
 *
 * @param bytes byte[]
 */
public void setBytes(byte[] bytes) {
	bytes_ = bytes;
}
/**
 * Sets the bytes comprising the translated data from the given string.
 * <p>
 * The translated bytes are derived by converting the string to IBM i bytes
 * using the given <i>ccsid</i>. The <i>as400</i> is required to perform the
 * conversion from text to bytes. A ccsid of 0 indicates to use the ccsid
 * of the current user.
 *
 * @param s java.lang.String
 * @param ccsid int
 * @param as400 com.ibm.as400.access.AS400
 */
public void setBytes(String s, int ccsid, AS400 as400) {
	// if 0, store the current ccsid since it will be used in the conversion
	if (ccsid == 0)
		ccsid = as400.getCcsid();
	setCcsid(ccsid);

	// check for unicode ccsid (no conversion required)
	if (ccsid == 13488) {
		setBytes(s.getBytes());
		return;
	}

	// assume a maximum buffer of (2 * length of the string) to allow for double-byte
	// the buffer will automatically be padded with blanks during conversion
	// blanks are then trimmed
	int len = s.length() * 2;
	byte[] buffer = new AS400Text(len, ccsid, as400).toBytes(s);
	while (len > 0)
		if (buffer[len-1] != 0x40) break;
			else len--;

	byte[] trimmed = new byte[len];
	System.arraycopy(buffer, 0, trimmed, 0, len);
	setBytes(trimmed);
}
/**
 * Sets the coded character set identifier used to encode the translated bytes.
 * <p>
 * Valid CCSID values are in the range 1 through 65535. The special value 0
 * can be used to indicate the default CCSID for the current user (when
 * the validation list APIs are invoked on the system). In some cases,
 * primarily attribute data, the special value -1 is also allowed. This
 * indicates that no CCSID value is stored with the data (i.e. binary data,
 * where no conversion is required).
 *
 * @param ccsid int
 */
public void setCcsid(int ccsid) {
	// Note: Compensate for IBM HTTP Internet User tools, which appear to
	// sometimes insert entries with invalid CCSID identifiers.
	// If not valid, assume 37 as a default.
	// CCSIDs <= 0 are sometimes used to indicate special values
	// when tagging attribute data, etc.
	ccsid_ = (ccsid <= 0 || isValidCcsid(ccsid)) ? ccsid : 37;
}
/**
 * Returns the byte array resulting from converting this object to a structure
 * usable by the system APIs.
 *
 * @return byte[]
 */
public byte[] toBytes() {
	byte[] buffer =
		new byte[getByteLength()];
	toBytes(buffer, 0);
	return buffer;
}
/**
 * Converts this object to a byte structure usable by the system APIs.
 * <p>
 * The IBM i bytes are inserted into the <i>buffer</i> starting at the given
 * <i>offset</i>. The total number of bytes inserted is returned.
 *
 * @param buffer byte[]
 * @param offset int
 * @return int
 */
public int toBytes(byte[] buffer, int offset) {
	byte[] bytes = getBytes();
	int byteLength = (bytes == null) ? getWriteNullDataLength() : bytes.length;

	// write length of translated bytes
	new AS400Bin4().toBytes(byteLength, buffer, offset + getWriteOffsetTByteLength());
	// write ccsid
	new AS400Bin4().toBytes(getCcsid(), buffer, offset + getWriteOffsetCcsid());
	// write translated bytes
	if (byteLength > 0)
		System.arraycopy(
			bytes, 0, buffer, offset+getWriteOffsetTBytes(), byteLength);

	return getByteLength();
}
}