annotate lib/mod/src/de/masters_of_disaster/ant/tasks/ar/ArUtils.java @ 33:3d86f0391168

Work on improving the build system.
author David Barts <davidb@stashtea.com>
date Fri, 24 Apr 2020 19:45:57 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
1 package de.masters_of_disaster.ant.tasks.ar;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
2
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
3 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
4 * This class provides static utility methods to work with byte streams.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
5 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
6 public class ArUtils {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
7 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
8 * Parse an octal string from a header buffer. This is used for the
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
9 * file permission mode value.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
10 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
11 * @param header The header buffer from which to parse.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
12 * @param offset The offset into the buffer from which to parse.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
13 * @param length The number of header bytes to parse.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
14 * @return The long value of the octal string.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
15 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
16 public static long parseOctal(byte[] header, int offset, int length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
17 long result = 0;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
18 int end = offset + length;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
19
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
20 for (int i=offset ; i<end ; i++) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
21 if (header[i] == (byte) ' ') {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
22 break;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
23 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
24 result = (result << 3) + (header[i] - '0');
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
25 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
26
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
27 return result;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
28 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
29
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
30 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
31 * Parse an entry name from a header buffer.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
32 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
33 * @param header The header buffer from which to parse.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
34 * @param offset The offset into the buffer from which to parse.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
35 * @param length The number of header bytes to parse.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
36 * @return The header's entry name.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
37 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
38 public static StringBuffer parseName(byte[] header, int offset, int length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
39 StringBuffer result = new StringBuffer(length);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
40 int end = offset + length;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
41
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
42 for (int i=offset ; i<end ; i++) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
43 if (header[i] == ' ') {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
44 break;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
45 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
46
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
47 result.append((char) header[i]);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
48 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
49
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
50 return result;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
51 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
52
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
53 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
54 * Write a name into a byte array.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
55 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
56 * @param name The name to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
57 * @param buf The byte array into which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
58 * @param offset The offset into the buffer from which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
59 * @param length The number of header bytes to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
60 * @return The number of bytes written to the buffer.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
61 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
62 public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
63 int i;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
64 int c = name.length();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
65
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
66 for (i=0 ; i<length && i<c ; i++) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
67 buf[offset+i] = (byte) name.charAt(i);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
68 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
69
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
70 while (i<length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
71 buf[offset+i] = (byte) ' ';
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
72 i++;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
73 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
74
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
75 return offset + length;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
76 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
77
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
78 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
79 * Write a long value into a byte array.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
80 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
81 * @param value The value to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
82 * @param buf The byte array into which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
83 * @param offset The offset into the buffer from which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
84 * @param length The number of header bytes to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
85 * @return The number of bytes written to the buffer.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
86 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
87 public static int getLongBytes(long value, byte[] buf, int offset, int length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
88 int i;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
89 String tmp = Long.toString(value);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
90 int c = tmp.length();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
91
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
92 for (i=0 ; i<length && i<c ; i++) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
93 buf[offset+i] = (byte) tmp.charAt(i);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
94 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
95
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
96 while (i<length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
97 buf[offset+i] = (byte) ' ';
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
98 i++;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
99 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
100
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
101 return offset + length;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
102 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
103
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
104 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
105 * Write an int value into a byte array.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
106 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
107 * @param value The value to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
108 * @param buf The byte array into which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
109 * @param offset The offset into the buffer from which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
110 * @param length The number of header bytes to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
111 * @return The number of bytes written to the buffer.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
112 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
113 public static int getIntegerBytes(int value, byte[] buf, int offset, int length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
114 int i;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
115 String tmp = Integer.toString(value);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
116 int c = tmp.length();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
117
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
118 for (i=0 ; i<length && i<c ; i++) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
119 buf[offset+i] = (byte) tmp.charAt(i);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
120 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
121
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
122 while (i<length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
123 buf[offset+i] = (byte) ' ';
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
124 i++;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
125 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
126
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
127 return offset + length;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
128 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
129
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
130 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
131 * Write an octal value into a byte array.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
132 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
133 * @param value The value to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
134 * @param buf The byte array into which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
135 * @param offset The offset into the buffer from which to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
136 * @param length The number of header bytes to write.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
137 * @return The number of bytes written to the buffer.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
138 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
139 public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
140 int i;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
141 String tmp = Long.toOctalString(value);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
142 int c = tmp.length();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
143
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
144 for (i=0 ; i<length && i<c ; i++) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
145 buf[offset+i] = (byte) tmp.charAt(i);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
146 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
147
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
148 while (i<length) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
149 buf[offset+i] = (byte) ' ';
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
150 i++;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
151 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
152
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
153 return offset + length;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
154 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
155 }