3
|
1 /**
|
|
2 * Title: tn5250J
|
|
3 * Copyright: Copyright (c) 2001
|
|
4 * Company:
|
|
5 * @author Kenneth J. Pouncey
|
|
6 * @version 0.4
|
|
7 *
|
|
8 * Description:
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2, or (at your option)
|
|
13 * any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this software; see the file COPYING. If not, write to
|
|
22 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
23 * Boston, MA 02111-1307 USA
|
|
24 *
|
|
25 */
|
|
26 package org.tn5250j.framework.tn5250;
|
|
27
|
|
28 public class Stream5250 {
|
|
29
|
|
30 public int streamSize;
|
|
31 public int opCode;
|
|
32 public int dataStart;
|
|
33 public int pos;
|
|
34 public byte buffer[];
|
|
35
|
|
36 public Stream5250(byte abyte0[]) {
|
|
37 buffer = abyte0;
|
|
38 // size without end of record 0xFF 0xEF
|
|
39 streamSize = (abyte0[0] & 0xff) << 8 | abyte0[1] & 0xff;
|
|
40 opCode = abyte0[9];
|
|
41 dataStart = 6 + abyte0[6];
|
|
42 pos = dataStart;
|
|
43 }
|
|
44
|
|
45 public Stream5250() {
|
112
|
46 buffer = null;
|
|
47 streamSize = 0;
|
|
48 opCode = 0;
|
|
49 dataStart = 0;
|
|
50 pos = dataStart;
|
|
51 }
|
3
|
52
|
|
53 /**
|
|
54 * This method takes a byte array and initializes the object information
|
|
55 * to be used.
|
112
|
56 *
|
3
|
57 * @param abyte0
|
|
58 */
|
|
59 public void initialize(byte abyte0[]) {
|
112
|
60 buffer = abyte0;
|
|
61 // size without end of record 0xFF 0xEF
|
|
62 streamSize = (abyte0[0] & 0xff) << 8 | abyte0[1] & 0xff;
|
|
63 opCode = abyte0[9];
|
|
64 dataStart = 6 + abyte0[6];
|
|
65 pos = dataStart;
|
3
|
66 }
|
112
|
67
|
3
|
68 public final int getOpCode() {
|
|
69 return opCode;
|
|
70 }
|
|
71
|
|
72 public final byte getNextByte()
|
112
|
73 throws Exception {
|
|
74 if (buffer == null || pos > buffer.length)
|
3
|
75 throw new Exception("Buffer length exceeded: " + pos);
|
|
76 else
|
|
77 return buffer[pos++];
|
|
78 }
|
|
79
|
|
80 public final void setPrevByte()
|
112
|
81 throws Exception {
|
|
82 if (pos == 0) {
|
3
|
83 throw new Exception("Index equals zero.");
|
|
84 }
|
|
85 else {
|
|
86 pos--;
|
|
87 return;
|
112
|
88 }
|
|
89 }
|
3
|
90
|
112
|
91 /**
|
|
92 * Returns where we are in the buffer
|
|
93 * @return position in the buffer
|
|
94 */
|
|
95 public final int getCurrentPos() {
|
|
96 return pos;
|
|
97 }
|
3
|
98
|
112
|
99 public final byte getByteOffset(int off)
|
|
100 throws Exception {
|
|
101 if (buffer == null || (pos + off) > buffer.length)
|
3
|
102 throw new Exception("Buffer length exceeded: " + pos);
|
|
103 else
|
|
104 return buffer[pos + off];
|
112
|
105 }
|
3
|
106
|
112
|
107 public final boolean size() {
|
|
108 return pos >= streamSize;
|
|
109 }
|
3
|
110
|
|
111
|
112
|
112 /**
|
|
113 * Determines if any more bytes are available in the buffer to be processed.
|
|
114 * @return yes or no
|
|
115 */
|
|
116 public final boolean hasNext() {
|
3
|
117 // return pos >= buffer.length;
|
112
|
118 return pos < streamSize;
|
|
119 }
|
3
|
120
|
112
|
121 /**
|
|
122 * This routine will retrieve a segment based on the first two bytes being
|
|
123 * the length of the segment.
|
|
124 *
|
|
125 * @return a new byte array containing the bytes of the segment.
|
|
126 * @throws Exception
|
|
127 */
|
|
128 public final byte[] getSegment() throws Exception {
|
|
129 // The first two bytes contain the length of the segment.
|
|
130 int length = ((buffer[pos] & 0xff) << 8 | (buffer[pos + 1] & 0xff));
|
|
131 // allocate space for it.
|
|
132 byte[] segment = new byte[length];
|
|
133 getSegment(segment, length, true);
|
|
134 return segment;
|
|
135 }
|
3
|
136
|
|
137
|
112
|
138 /**
|
|
139 * This routine will retrieve a byte array based on the first two bytes being
|
|
140 * the length of the segment.
|
|
141 *
|
|
142 * @param segment - byte array
|
|
143 * @param length - length of segment to return
|
|
144 * @param adjustPos - adjust the position of the buffer to the end of the seg
|
|
145 * ment
|
|
146 * @throws Exception
|
|
147 */
|
|
148 public final void getSegment(byte[] segment, int length, boolean adjustPos)
|
|
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);
|
3
|
154
|
112
|
155 // use the system array copy to move the bytes from the buffer
|
|
156 // to the allocated byte array
|
|
157 System.arraycopy(buffer, pos, segment, 0, length);
|
3
|
158
|
112
|
159 // update the offset to be after the segment so the next byte can be read
|
|
160 if (adjustPos)
|
|
161 pos += length;
|
|
162 }
|
3
|
163
|
|
164 }
|