Mercurial > 510Connectbot
annotate app/src/main/java/org/tn5250j/framework/tn5250/Stream5250.java @ 454:7492d2cb7b75
update to latest toolchain, gradle 4.10.2
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 07 Nov 2018 17:13:10 -0800 |
parents | 8fa8e73e2f5c |
children |
rev | line source |
---|---|
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 { | |
445
8fa8e73e2f5c
update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
29 public static final int OPCODE_OFFSET = 9; |
3 | 30 |
31 public int streamSize; | |
32 public int opCode; | |
33 public int dataStart; | |
34 public int pos; | |
35 public byte buffer[]; | |
36 | |
37 public Stream5250(byte abyte0[]) { | |
38 buffer = abyte0; | |
39 // size without end of record 0xFF 0xEF | |
40 streamSize = (abyte0[0] & 0xff) << 8 | abyte0[1] & 0xff; | |
41 opCode = abyte0[9]; | |
42 dataStart = 6 + abyte0[6]; | |
43 pos = dataStart; | |
44 } | |
45 | |
46 public Stream5250() { | |
112 | 47 buffer = null; |
48 streamSize = 0; | |
49 opCode = 0; | |
50 dataStart = 0; | |
51 pos = dataStart; | |
52 } | |
3 | 53 |
54 /** | |
55 * This method takes a byte array and initializes the object information | |
56 * to be used. | |
112 | 57 * |
3 | 58 * @param abyte0 |
59 */ | |
60 public void initialize(byte abyte0[]) { | |
112 | 61 buffer = abyte0; |
62 // size without end of record 0xFF 0xEF | |
63 streamSize = (abyte0[0] & 0xff) << 8 | abyte0[1] & 0xff; | |
445
8fa8e73e2f5c
update to tn5250j version 0.7.7, svn r1270
Carl Byington <carl@five-ten-sg.com>
parents:
438
diff
changeset
|
64 opCode = abyte0[OPCODE_OFFSET]; |
112 | 65 dataStart = 6 + abyte0[6]; |
66 pos = dataStart; | |
3 | 67 } |
112 | 68 |
3 | 69 public final int getOpCode() { |
70 return opCode; | |
71 } | |
72 | |
73 public final byte getNextByte() | |
112 | 74 throws Exception { |
75 if (buffer == null || pos > buffer.length) | |
3 | 76 throw new Exception("Buffer length exceeded: " + pos); |
77 else | |
78 return buffer[pos++]; | |
79 } | |
80 | |
81 public final void setPrevByte() | |
112 | 82 throws Exception { |
83 if (pos == 0) { | |
3 | 84 throw new Exception("Index equals zero."); |
85 } | |
86 else { | |
87 pos--; | |
88 return; | |
112 | 89 } |
90 } | |
3 | 91 |
112 | 92 /** |
93 * Returns where we are in the buffer | |
94 * @return position in the buffer | |
95 */ | |
96 public final int getCurrentPos() { | |
97 return pos; | |
98 } | |
3 | 99 |
112 | 100 public final byte getByteOffset(int off) |
101 throws Exception { | |
102 if (buffer == null || (pos + off) > buffer.length) | |
3 | 103 throw new Exception("Buffer length exceeded: " + pos); |
104 else | |
105 return buffer[pos + off]; | |
112 | 106 } |
3 | 107 |
112 | 108 public final boolean size() { |
109 return pos >= streamSize; | |
110 } | |
3 | 111 |
112 | |
112 | 113 /** |
114 * Determines if any more bytes are available in the buffer to be processed. | |
115 * @return yes or no | |
116 */ | |
117 public final boolean hasNext() { | |
3 | 118 // return pos >= buffer.length; |
112 | 119 return pos < streamSize; |
120 } | |
3 | 121 |
112 | 122 /** |
123 * This routine will retrieve a segment based on the first two bytes being | |
124 * the length of the segment. | |
125 * | |
126 * @return a new byte array containing the bytes of the segment. | |
127 * @throws Exception | |
128 */ | |
129 public final byte[] getSegment() throws Exception { | |
130 // The first two bytes contain the length of the segment. | |
131 int length = ((buffer[pos] & 0xff) << 8 | (buffer[pos + 1] & 0xff)); | |
132 // allocate space for it. | |
133 byte[] segment = new byte[length]; | |
134 getSegment(segment, length, true); | |
135 return segment; | |
136 } | |
3 | 137 |
138 | |
112 | 139 /** |
140 * This routine will retrieve a byte array based on the first two bytes being | |
141 * the length of the segment. | |
142 * | |
143 * @param segment - byte array | |
144 * @param length - length of segment to return | |
145 * @param adjustPos - adjust the position of the buffer to the end of the seg | |
146 * ment | |
147 * @throws Exception | |
148 */ | |
149 public final void getSegment(byte[] segment, int length, boolean adjustPos) | |
150 throws Exception { | |
151 // If the length is larger than what is available throw an exception | |
152 if ((pos + length) > buffer.length) | |
153 throw new Exception("Buffer length exceeded: start " + pos | |
154 + " length " + length); | |
3 | 155 |
112 | 156 // use the system array copy to move the bytes from the buffer |
157 // to the allocated byte array | |
158 System.arraycopy(buffer, pos, segment, 0, length); | |
3 | 159 |
112 | 160 // update the offset to be after the segment so the next byte can be read |
161 if (adjustPos) | |
162 pos += length; | |
163 } | |
3 | 164 |
165 } |