comparison src/org/tn5250j/framework/tn5250/Stream5250.java @ 112:77ac18bc1b2f

cleanup java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 18 Jun 2014 13:03:01 -0700
parents e8d2a24e85c6
children
comparison
equal deleted inserted replaced
111:6a0ad4d384ea 112:77ac18bc1b2f
41 dataStart = 6 + abyte0[6]; 41 dataStart = 6 + abyte0[6];
42 pos = dataStart; 42 pos = dataStart;
43 } 43 }
44 44
45 public Stream5250() { 45 public Stream5250() {
46 buffer = null; 46 buffer = null;
47 streamSize = 0; 47 streamSize = 0;
48 opCode = 0; 48 opCode = 0;
49 dataStart = 0; 49 dataStart = 0;
50 pos = dataStart; 50 pos = dataStart;
51 } 51 }
52 52
53 /** 53 /**
54 * This method takes a byte array and initializes the object information 54 * This method takes a byte array and initializes the object information
55 * to be used. 55 * to be used.
56 * 56 *
57 * @param abyte0 57 * @param abyte0
58 */ 58 */
59 public void initialize(byte abyte0[]) { 59 public void initialize(byte abyte0[]) {
60 60 buffer = abyte0;
61 buffer = abyte0; 61 // size without end of record 0xFF 0xEF
62 // size without end of record 0xFF 0xEF 62 streamSize = (abyte0[0] & 0xff) << 8 | abyte0[1] & 0xff;
63 streamSize = (abyte0[0] & 0xff) << 8 | abyte0[1] & 0xff; 63 opCode = abyte0[9];
64 opCode = abyte0[9]; 64 dataStart = 6 + abyte0[6];
65 dataStart = 6 + abyte0[6]; 65 pos = dataStart;
66 pos = dataStart;
67
68 } 66 }
69 67
70 public final int getOpCode() { 68 public final int getOpCode() {
71 return opCode; 69 return opCode;
72 } 70 }
73 71
74 public final byte getNextByte() 72 public final byte getNextByte()
75 throws Exception { 73 throws Exception {
76 if(buffer == null || pos > buffer.length) 74 if (buffer == null || pos > buffer.length)
77 throw new Exception("Buffer length exceeded: " + pos); 75 throw new Exception("Buffer length exceeded: " + pos);
78 else 76 else
79 return buffer[pos++]; 77 return buffer[pos++];
80 } 78 }
81 79
82 public final void setPrevByte() 80 public final void setPrevByte()
83 throws Exception { 81 throws Exception {
84 if(pos == 0) { 82 if (pos == 0) {
85 throw new Exception("Index equals zero."); 83 throw new Exception("Index equals zero.");
86 } 84 }
87 else { 85 else {
88 pos--; 86 pos--;
89 return; 87 return;
90 } 88 }
91 } 89 }
92 90
93 /** 91 /**
94 * Returns where we are in the buffer 92 * Returns where we are in the buffer
95 * @return position in the buffer 93 * @return position in the buffer
96 */ 94 */
97 public final int getCurrentPos() { 95 public final int getCurrentPos() {
98 return pos; 96 return pos;
99 } 97 }
100 98
101 public final byte getByteOffset(int off) 99 public final byte getByteOffset(int off)
102 throws Exception { 100 throws Exception {
103 101 if (buffer == null || (pos + off) > buffer.length)
104 if(buffer == null || (pos + off ) > buffer.length)
105 throw new Exception("Buffer length exceeded: " + pos); 102 throw new Exception("Buffer length exceeded: " + pos);
106 else 103 else
107 return buffer[pos + off]; 104 return buffer[pos + off];
105 }
108 106
109 } 107 public final boolean size() {
110 108 return pos >= streamSize;
111 public final boolean size() { 109 }
112 return pos >= streamSize;
113 }
114 110
115 111
116 /** 112 /**
117 * Determines if any more bytes are available in the buffer to be processed. 113 * Determines if any more bytes are available in the buffer to be processed.
118 * @return yes or no 114 * @return yes or no
119 */ 115 */
120 public final boolean hasNext() { 116 public final boolean hasNext() {
117 // return pos >= buffer.length;
118 return pos < streamSize;
119 }
121 120
122 // return pos >= buffer.length; 121 /**
123 return pos < streamSize; 122 * This routine will retrieve a segment based on the first two bytes being
124 } 123 * the length of the segment.
125 124 *
126 /** 125 * @return a new byte array containing the bytes of the segment.
127 * This routine will retrieve a segment based on the first two bytes being 126 * @throws Exception
128 * the length of the segment. 127 */
129 * 128 public final byte[] getSegment() throws Exception {
130 * @return a new byte array containing the bytes of the segment. 129 // The first two bytes contain the length of the segment.
131 * @throws Exception 130 int length = ((buffer[pos] & 0xff) << 8 | (buffer[pos + 1] & 0xff));
132 */ 131 // allocate space for it.
133 public final byte[] getSegment() throws Exception { 132 byte[] segment = new byte[length];
134 133 getSegment(segment, length, true);
135 // The first two bytes contain the length of the segment. 134 return segment;
136 int length = ((buffer[pos] & 0xff )<< 8 | (buffer[pos+1] & 0xff)); 135 }
137 // allocate space for it.
138 byte[] segment = new byte[length];
139
140 getSegment(segment,length,true);
141
142 return segment;
143 }
144 136
145 137
146 /** 138 /**
147 * This routine will retrieve a byte array based on the first two bytes being 139 * This routine will retrieve a byte array based on the first two bytes being
148 * the length of the segment. 140 * the length of the segment.
149 * 141 *
150 * @param segment - byte array 142 * @param segment - byte array
151 * @param length - length of segment to return 143 * @param length - length of segment to return
152 * @param adjustPos - adjust the position of the buffer to the end of the seg 144 * @param adjustPos - adjust the position of the buffer to the end of the seg
153 * ment 145 * ment
154 * @throws Exception 146 * @throws Exception
155 */ 147 */
156 public final void getSegment(byte[] segment, int length, boolean adjustPos) 148 public final void getSegment(byte[] segment, int length, boolean adjustPos)
157 throws Exception { 149 throws Exception {
150 // If the length is larger than what is available throw an exception
151 if ((pos + length) > buffer.length)
152 throw new Exception("Buffer length exceeded: start " + pos
153 + " length " + length);
158 154
159 // If the length is larger than what is available throw an exception 155 // use the system array copy to move the bytes from the buffer
160 if((pos + length ) > buffer.length) 156 // to the allocated byte array
161 throw new Exception("Buffer length exceeded: start " + pos 157 System.arraycopy(buffer, pos, segment, 0, length);
162 + " length " + length);
163 // use the system array copy to move the bytes from the buffer
164 // to the allocated byte array
165 System.arraycopy(buffer,pos,segment,0,length);
166 158
167 // update the offset to be after the segment so the next byte can be read 159 // update the offset to be after the segment so the next byte can be read
168 if (adjustPos) 160 if (adjustPos)
169 pos +=length; 161 pos += length;
170 162 }
171 }
172 163
173 } 164 }