comparison src/org/tn5250j/framework/tn5250/Stream5250.java @ 3:e8d2a24e85c6 tn5250

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