forked from mbtaylor/jcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecord.java
More file actions
222 lines (202 loc) · 6.52 KB
/
Record.java
File metadata and controls
222 lines (202 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package uk.ac.bristol.star.cdf.record;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
/**
* Abstract superclass for a CDF Record object.
* A Record represents one of the sequence of typed records
* of which a CDF file is composed.
*
* @author Mark Taylor
* @since 18 Jun 2013
*/
public abstract class Record {
private final RecordPlan plan_;
private final String abbrev_;
private final Logger logger_ = Logger.getLogger( Record.class.getName() );
/**
* Constructs a record with no known record type.
*
* @param plan basic record information
* @param abbrev abreviated name for record type
*/
protected Record( RecordPlan plan, String abbrev ) {
plan_ = plan;
abbrev_ = abbrev;
}
/**
* Constructs a record with a known record type.
*
* @param plan basic record information
* @param abbrev abreviated name for record type
* @param fixedType record type asserted for this record
*/
protected Record( RecordPlan plan, String abbrev, int fixedType ) {
this( plan, abbrev );
int planType = plan.getRecordType();
// This really shouldn't happen.
if ( planType != fixedType ) {
throw new AssertionError( "Incorrect record type ("
+ planType + " != " + fixedType + ")" );
}
}
/**
* Returns the size of the record in bytes.
*
* @return record size
*/
public long getRecordSize() {
return plan_.getRecordSize();
}
/**
* Returns the type code identifying what kind of CDF record it is.
*
* @return record type
*/
public int getRecordType() {
return plan_.getRecordType();
}
/**
* Returns the buffer containing the record data.
*
* @return buffer
*/
public Buf getBuf() {
return plan_.getBuf();
}
/**
* Returns the abbreviated form of the record type for this record.
*
* @return record type abbreviation
*/
public String getRecordTypeAbbreviation() {
return abbrev_;
}
/**
* Returns the buffer offset of the first field in this record after
* the record size and type values.
*
* @return buffer offset for non-generic record content
*/
public long getContentOffset() {
return plan_.createContentPointer().get();
}
/**
* Checks that an integer has a known fixed value.
* If not, a warning may be emitted.
* This performs an assertion-like function.
* The actual value is returned as a convenience.
*
* @param actualValue value to test
* @param fixedValue value to compare against
* @return <code>actualValue</code>
*/
protected int checkIntValue( int actualValue, int fixedValue ) {
if ( actualValue != fixedValue ) {
warnFormat( "Unexpected fixed value " + actualValue + " != "
+ fixedValue );
}
return actualValue;
}
/**
* Checks that a pointer is positioned at the end of this record.
* If not, a warning may be emitted.
* This performs an assertion-like function.
* This can be called by code which thinks it has read a whole record's
* content to check that it's got the counting right.
*
* @param ptr pointer notionally positioned at end of record
*/
protected void checkEndRecord( Pointer ptr ) {
long readCount = plan_.getReadCount( ptr );
long recSize = getRecordSize();
if ( readCount != recSize ) {
warnFormat( "Bytes read in record not equal to record size ("
+ readCount + " != " + recSize + ")" );
}
}
/**
* Called by <code>check*</code> methods to issue a warning if the
* check has failed.
*
* @param msg message to output
*/
protected void warnFormat( String msg ) {
assert false : msg;
logger_.warning( msg );
}
/**
* Reads a moderately-sized array of 4-byte big-endian integers.
* Pointer position is moved on appropriately.
* Not intended for potentially very large arrays.
*
* @param buf buffer
* @param ptr pointer
* @param count number of values to read
* @return <code>count</code>-element array of values
*/
public static int[] readIntArray( Buf buf, Pointer ptr, int count )
throws IOException {
int[] array = new int[ count ];
for ( int i = 0; i < count; i++ ) {
array[ i ] = buf.readInt( ptr );
}
return array;
}
/**
* Reads a moderately-sized offset 8-byte big-endian integers.
* Pointer position is moved on appropriately.
* Not intended for potentially very large arrays.
*
* @param buf buffer
* @param ptr pointer
* @param count number of values to read
* @return <code>count</code>-element array of values
*/
public static long[] readOffsetArray( Buf buf, Pointer ptr, int count )
throws IOException {
long[] array = new long[ count ];
for ( int i = 0; i < count; i++ ) {
array[ i ] = buf.readOffset( ptr );
}
return array;
}
/**
* Splits an ASCII string into 0x0A-terminated lines.
*
* @param text string containing ASCII characters
* @return array of lines split on linefeeds
*/
public static String[] toLines( String text ) {
List<String> lines = new ArrayList<String>();
// Line ends in regexes are so inscrutable that use of String.split()
// seems too much trouble. See Goldfarb's First Law Of Text
// Processing.
int nc = text.length();
StringBuilder sbuf = new StringBuilder( nc );
for ( int i = 0; i < nc; i++ ) {
char c = text.charAt( i );
if ( c == 0x0a ) {
lines.add( sbuf.toString() );
sbuf.setLength( 0 );
}
else {
sbuf.append( c );
}
}
if ( sbuf.length() > 0 ) {
lines.add( sbuf.toString() );
}
return lines.toArray( new String[ 0 ] );
}
/**
* Indicates whether a given bit of a flags mask is set.
*
* @param flags flags mask
* @param ibit bit index; 0 is the least significant
*/
public static boolean hasBit( int flags, int ibit ) {
return ( flags >> ibit ) % 2 == 1;
}
}