01: package com.mockrunner.example.connector;
02:
03: import java.io.ByteArrayOutputStream;
04:
05: /**
06: * Represents a person.
07: */
08: public class Person extends DomainObjectRecord {
09: private String id;
10: private String firstName;
11: private String lastName;
12: private int age;
13:
14: public String getId() {
15: return id;
16: }
17:
18: public void setId(String id) {
19: this .id = id;
20: }
21:
22: public String getFirstName() {
23: return firstName;
24: }
25:
26: public void setFirstName(String firstName) {
27: this .firstName = firstName;
28: }
29:
30: public String getLastName() {
31: return lastName;
32: }
33:
34: public void setLastName(String lastName) {
35: this .lastName = lastName;
36: }
37:
38: public int getAge() {
39: return age;
40: }
41:
42: public void setAge(int age) {
43: this .age = age;
44: }
45:
46: public byte[] marshal() {
47: ByteArrayOutputStream stream = new ByteArrayOutputStream();
48: byte[] encodedId = Marshaller.marshalString(id, 8);
49: byte[] encodedFirstName = Marshaller.marshalString(firstName,
50: 10);
51: byte[] encodedLastName = Marshaller.marshalString(lastName, 10);
52: byte[] encodedAge = Marshaller.marshalNumber(age);
53: stream.write(encodedId, 0, encodedId.length);
54: stream.write(encodedFirstName, 0, encodedFirstName.length);
55: stream.write(encodedLastName, 0, encodedLastName.length);
56: stream.write(encodedAge, 0, encodedAge.length);
57: return stream.toByteArray();
58: }
59:
60: public void unmarshal(byte[] data) {
61: byte[] encodedId = new byte[8];
62: byte[] encodedFirstName = new byte[10];
63: byte[] encodedLastName = new byte[10];
64: byte[] encodedAge = new byte[4];
65: System.arraycopy(data, 0, encodedId, 0, 8);
66: System.arraycopy(data, 8, encodedFirstName, 0, 10);
67: System.arraycopy(data, 18, encodedLastName, 0, 10);
68: System.arraycopy(data, 28, encodedAge, 0, 4);
69: id = Marshaller.unmarshalString(encodedId).trim();
70: firstName = Marshaller.unmarshalString(encodedFirstName).trim();
71: lastName = Marshaller.unmarshalString(encodedLastName).trim();
72: age = Marshaller.unmarshalNumber(encodedAge);
73: }
74: }
|