Added a new property, characterEncoding, to MessageFactory, IsoMessage, FieldParseInfo and IsoValue. This helps in parsing messages from byte buffers containing extended characters in encodings other than the default for the JVM. Tested with UTF8 on a Mac (which has default Mac OS Roman) and worked fine.
Several methods now have an additional encoding parameter (just a String, not directly a Charset), but the default is always the JVM's default encoding (taken from the property 'file.encoding'). git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@70 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
@@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
package com.solab.iso8583.parse;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.text.ParseException;
|
||||
|
||||
import com.solab.iso8583.CustomField;
|
||||
@@ -34,7 +35,7 @@ public abstract class AlphaNumericFieldParseInfo extends FieldParseInfo {
|
||||
super(t, len);
|
||||
}
|
||||
|
||||
public <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom) throws ParseException {
|
||||
public <T extends Object> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom) throws ParseException, UnsupportedEncodingException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
}
|
||||
@@ -42,12 +43,16 @@ public abstract class AlphaNumericFieldParseInfo extends FieldParseInfo {
|
||||
throw new ParseException(String.format("Insufficient data for %s field of length %d, pos %d",
|
||||
type, length, pos), pos);
|
||||
}
|
||||
String _v = new String(buf, pos, length, getCharacterEncoding());
|
||||
if (_v.length() != length) {
|
||||
_v = new String(buf, pos, buf.length-pos, getCharacterEncoding()).substring(0, length);
|
||||
}
|
||||
if (custom == null) {
|
||||
return new IsoValue<String>(type, new String(buf, pos, length), length, null);
|
||||
return new IsoValue<String>(type, _v, length, null);
|
||||
} else {
|
||||
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(new String(buf, pos, length)), length, custom);
|
||||
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(_v), length, custom);
|
||||
if (v.getValue() == null) {
|
||||
return new IsoValue<String>(type, new String(buf, pos, length), length, null);
|
||||
return new IsoValue<String>(type, _v, length, null);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user