Mercurial > 510Connectbot
comparison app/src/main/java/org/tn5250j/framework/tn5250/Stream5250.java @ 438:d29cce60f393
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 11:23:55 -0800 |
parents | src/org/tn5250j/framework/tn5250/Stream5250.java@77ac18bc1b2f |
children | 8fa8e73e2f5c |
comparison
equal
deleted
inserted
replaced
437:208b31032318 | 438:d29cce60f393 |
---|---|
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 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; | |
66 } | |
67 | |
68 public final int getOpCode() { | |
69 return opCode; | |
70 } | |
71 | |
72 public final byte getNextByte() | |
73 throws Exception { | |
74 if (buffer == null || pos > buffer.length) | |
75 throw new Exception("Buffer length exceeded: " + pos); | |
76 else | |
77 return buffer[pos++]; | |
78 } | |
79 | |
80 public final void setPrevByte() | |
81 throws Exception { | |
82 if (pos == 0) { | |
83 throw new Exception("Index equals zero."); | |
84 } | |
85 else { | |
86 pos--; | |
87 return; | |
88 } | |
89 } | |
90 | |
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 } | |
98 | |
99 public final byte getByteOffset(int off) | |
100 throws Exception { | |
101 if (buffer == null || (pos + off) > buffer.length) | |
102 throw new Exception("Buffer length exceeded: " + pos); | |
103 else | |
104 return buffer[pos + off]; | |
105 } | |
106 | |
107 public final boolean size() { | |
108 return pos >= streamSize; | |
109 } | |
110 | |
111 | |
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() { | |
117 // return pos >= buffer.length; | |
118 return pos < streamSize; | |
119 } | |
120 | |
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 } | |
136 | |
137 | |
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); | |
154 | |
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); | |
158 | |
159 // update the offset to be after the segment so the next byte can be read | |
160 if (adjustPos) | |
161 pos += length; | |
162 } | |
163 | |
164 } |