Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COctetString update #33

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions jsmpp/src/main/java/org/jsmpp/bean/OptionalParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,32 @@ protected byte[] serializeValue() {
public static class COctetString extends OctetString {

public COctetString(short tag, String value, String charsetName)
throws UnsupportedEncodingException {
super(tag, value, charsetName);
}
throws UnsupportedEncodingException {
super(tag, new byte[value.getBytes(charsetName).length + 1]);
System.arraycopy(value.getBytes(charsetName), 0, this.value, 0,
value.getBytes(charsetName).length);
this.value[value.getBytes().length] = (byte) 0x00;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not passing the charsetName argument to getBytes() on that line

You call value.getBytes(charsetName) many times, can you revise the code to call it just once?

}

public COctetString(short tag, String value) {
super(tag, new byte[value.getBytes().length + 1]);
System.arraycopy(value.getBytes(), 0, this.value, 0,
value.getBytes().length);
this.value[value.getBytes().length] = (byte) 0x00;
return;
}

public COctetString(short tag, byte[] value) {
super(tag, value);
}

@Override
public String getValueAsString() {
byte[] s = new byte[(value.length > 0 ? value.length - 1 : 0)];
System.arraycopy(value, 0, s, 0, s.length);
return new String(s);
}

public COctetString(short tag, String value) {
super(tag, value);
}

public COctetString(short tag, byte[] value) {
super(tag, value);
}

@Override
public String getValueAsString() {
return new String(getValue());
}

}

/**
Expand Down Expand Up @@ -2150,6 +2159,9 @@ private Vendor_specific_msc_addr(short tag, byte value[]) {
super(tag, value);
try {
address = new String(value, 2, value.length-2, "ISO-8859-1");
} catch (StringIndexOutOfBoundsException e) {
// TODO: do something better
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO: do something better
e.printStackTrace();
Expand Down
9 changes: 9 additions & 0 deletions jsmpp/src/test/java/org/jsmpp/bean/OptionalParameterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,14 @@ public void cOctetStringGetValueAsString() throws UnsupportedEncodingException {
COctetString string = new OptionalParameter.COctetString(Tag.ADDITIONAL_STATUS_INFO_TEXT.code(), "urgent");

assertEquals(string.getValueAsString(), "urgent");

assertEquals((byte)0x75, string.getValue()[0]); // u
assertEquals((byte)0x72, string.getValue()[1]); // r
assertEquals((byte)0x67, string.getValue()[2]); // g
assertEquals((byte)0x65, string.getValue()[3]); // e
assertEquals((byte)0x6e, string.getValue()[4]); // n
assertEquals((byte)0x74, string.getValue()[5]); // t
assertEquals((byte)0x00, string.getValue()[6]); // NULL

}
}