summaryrefslogtreecommitdiff
path: root/smccTest/src/main/java/at/gv/egiz/smcc/util
diff options
context:
space:
mode:
Diffstat (limited to 'smccTest/src/main/java/at/gv/egiz/smcc/util')
-rw-r--r--smccTest/src/main/java/at/gv/egiz/smcc/util/TLV.java84
-rw-r--r--smccTest/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java83
2 files changed, 167 insertions, 0 deletions
diff --git a/smccTest/src/main/java/at/gv/egiz/smcc/util/TLV.java b/smccTest/src/main/java/at/gv/egiz/smcc/util/TLV.java
new file mode 100644
index 00000000..b67fe3fa
--- /dev/null
+++ b/smccTest/src/main/java/at/gv/egiz/smcc/util/TLV.java
@@ -0,0 +1,84 @@
+package at.gv.egiz.smcc.util;
+
+
+
+/*
+ * Copyright 2009 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class TLV {
+
+ private byte[] bytes;
+ private int start;
+
+ public TLV(byte[] bytes, int start) {
+ if (bytes.length - start < 2) {
+ throw new IllegalArgumentException("TLV must at least consit of tag and length.");
+ }
+ this.bytes = bytes;
+ this.start = start;
+ }
+
+ /**
+ * @return the tag
+ */
+ public int getTag() {
+ return 0xFF & bytes[start];
+ }
+
+ /**
+ * @return the length
+ */
+ public int getLength() {
+ return 0xFF & bytes[start + 1];
+ }
+
+ /**
+ * @return the value
+ */
+ public byte[] getValue() {
+ byte[] value = new byte[getLength()];
+ System.arraycopy(bytes, start + 2, value, 0, value.length);
+ return value;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "Tag = " + Integer.toHexString(getTag()) + ", Length = " + getLength() + ", Value = " + toString(getValue());
+ }
+
+ public static String toString(byte[] b) {
+ StringBuffer sb = new StringBuffer();
+ sb.append('[');
+ if (b != null && b.length > 0) {
+ sb.append(Integer.toHexString((b[0] & 240) >> 4));
+ sb.append(Integer.toHexString(b[0] & 15));
+ for (int i = 1; i < b.length; i++) {
+ sb.append((i % 32 == 0) ? '\n' : ':');
+ sb.append(Integer.toHexString((b[i] & 240) >> 4));
+ sb.append(Integer.toHexString(b[i] & 15));
+ }
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+
+
+}
diff --git a/smccTest/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java b/smccTest/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java
new file mode 100644
index 00000000..fc29a7e7
--- /dev/null
+++ b/smccTest/src/main/java/at/gv/egiz/smcc/util/TLVSequence.java
@@ -0,0 +1,83 @@
+package at.gv.egiz.smcc.util;
+
+
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/*
+ * Copyright 2009 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class TLVSequence implements Iterable<TLV> {
+
+ private byte[] bytes;
+
+ public TLVSequence(byte[] bytes) {
+ this.bytes = bytes;
+ }
+
+ @Override
+ public Iterator<TLV> iterator() {
+ return new TLVIterator();
+ }
+
+ public byte[] getValue(int tag) {
+ for (TLV tlv : this) {
+ if (tlv.getTag() == tag) {
+ return tlv.getValue();
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ for (TLV tlv : this) {
+ sb.append(tlv).append('\n');
+ }
+ return sb.toString();
+ }
+
+ private class TLVIterator implements Iterator<TLV> {
+
+ private int pos = 0;
+
+ @Override
+ public boolean hasNext() {
+ return (bytes.length - pos > 2);
+ }
+
+ @Override
+ public TLV next() {
+ if (hasNext()) {
+ TLV tlv = new TLV(bytes, pos);
+ pos += tlv.getLength() + 2;
+ return tlv;
+ } else {
+ throw new NoSuchElementException();
+ }
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ }
+
+}