1 package info.mikethomas.fahview.v6project.utilities;
2
3 /*
4 * #%L
5 * This file is part of FAHView-v6project.
6 * %%
7 * Copyright (C) 2011 - 2017 Mike Thomas <mikepthomas@outlook.com>
8 * %%
9 * FAHView is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 * %
14 * FAHView is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * %
19 * You should have received a copy of the GNU General Public License
20 * along with FAHView. If not, see <http://www.gnu.org/licenses/>.
21 * #L%
22 */
23
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27
28 /**
29 * Class to extract data from Folding@home client queue files.
30 *
31 * @author <a href="mailto:mikepthomas@outlook.com">Michael Thomas</a>
32 * @version $Id: $Id
33 */
34 public class QueueReader {
35 private RandomAccessFile file;
36
37 /**
38 * <p>Constructor for QueueReader.</p>
39 *
40 * @param fileName a {@link java.lang.String} object.
41 */
42 public QueueReader(String fileName) {
43 try {
44 file = new RandomAccessFile(fileName, "r");
45 } catch (FileNotFoundException ex) {
46 System.err.println("File " + fileName + " Not Found");
47 }
48 }
49
50 /**
51 * get bytes from the queue file at the specified position
52 *
53 * @param position position in file to seek to
54 * @param length length in bytes to read
55 * @return byte[] raw bytes from file
56 */
57 public byte[] read(int position, int length) {
58 byte[] buf = new byte[length];
59 try {
60 file.seek(position);
61 file.read(buf);
62 } catch (IOException ex) {
63 System.err.println("Failed to read " + length + "bytes from position " + position);
64 }
65 return buf;
66 }
67
68 /**
69 * Read a Big Endian Unsigned Integer from the queue file
70 *
71 * @see <a href="http://darksleep.com/player/JavaAndUnsignedTypes.html">JavaAndUnsignedTypes</a>
72 * @param position position in file to seek to
73 * @param length length in bytes to read
74 * @return long Big Endian Unsigned Integer
75 */
76 public long readBEUInt(int position, int length) {
77 byte[] buf = this.read(position, length);
78
79 int index = 0;
80 int firstByte = (0xFF & ((int)buf[index]));
81 int secondByte = (0xFF & ((int)buf[index+1]));
82 int thirdByte = (0xFF & ((int)buf[index+2]));
83 int fourthByte = (0xFF & ((int)buf[index+3]));
84 return ((long) (firstByte << 24
85 | secondByte << 16
86 | thirdByte << 8
87 | fourthByte))
88 & 0xFFFFFFFFL;
89 }
90
91 /**
92 * Read a Little Endian Unsigned Integer from the queue file
93 *
94 * @param position position in file to seek to
95 * @param length length in bytes to read
96 * @return long Little Endian Unsigned Interger
97 */
98 public long readLEUInt(int position, int length) {
99 return ByteSwapper.swap((int) readBEUInt(position, length));
100 }
101
102 /**
103 * Read a Big Endian Unsigned Short from the queue file
104 *
105 * @see <a href="http://darksleep.com/player/JavaAndUnsignedTypes.html">JavaAndUnsignedTypes</a>
106 * @param position position in file to seek to
107 * @param length length in bytes to read
108 * @return int Big Endian Unsigned Short
109 */
110 public int readBEUShort(int position, int length) {
111 byte[] buf = this.read(position, length);
112
113 int index = 0;
114 int firstByte = (0xFF & ((int)buf[index]));
115 int secondByte = (0xFF & ((int)buf[index+1]));
116 return (char) (firstByte << 8 | secondByte);
117 }
118
119 /**
120 * Read a Little Endian Unsigned Short from the queue file
121 *
122 * @param position position in file to seek to
123 * @param length length in bytes to read
124 * @return long Little Endian Unsigned Short
125 */
126 public int readLEUShort(int position, int length) {
127 return ByteSwapper.swap((short) readBEUShort(position, length));
128 }
129
130 /**
131 * Read an IP Address from the queue file
132 *
133 * @param position position in file to seek to
134 * @return byte[] IP address in network order
135 */
136 public byte[] readIP(int position) {
137 byte[] buf = this.read(position, 4);
138 byte[] ip = new byte[buf.length];
139 ip[0] = buf[3];
140 ip[1] = buf[2];
141 ip[2] = buf[1];
142 ip[3] = buf[0];
143 return ip;
144 }
145
146 /**
147 * Read a String from the queue file
148 *
149 * @param position position in file to seek to
150 * @param length length in bytes to read
151 * @return String String of characters from file
152 */
153 public String readString(int position, int length) {
154 byte[] buf = this.read(position, length);
155 StringBuilder result = new StringBuilder();
156 for(int i = 0; i < buf.length; i++) {
157 if (buf[i] != 0) {
158 char c = (char)buf[i];
159 result.append(c);
160 }
161 }
162 return result.toString();
163 }
164 }