// since nothing we've done has invalidated it.
if (buffer == null) {
if (data == null) {
throw new FitsException(
"Attempt to write undefined ASCII Table");
}
buffer = new byte[nRows * rowLen];
bp = new ByteParser(buffer);
for (int i = 0; i < buffer.length; i += 1) {
buffer[i] = (byte) ' ';
}
ByteFormatter bf = new ByteFormatter();
bf.setTruncationThrow(false);
bf.setTruncateOnOverflow(true);
for (int i = 0; i < nRows; i += 1) {
for (int j = 0; j < nFields; j += 1) {
int offset = i * rowLen + offsets[j];
int len = lengths[j];
try {
if (isNull != null && isNull[i * nFields + j]) {
if (nulls[j] == null) {
throw new FitsException(
"No null value set when needed");
}
bf.format(nulls[j], buffer, offset, len);
} else {
if (types[j] == String.class) {
String[] s = (String[]) data[j];
bf.format(s[i], buffer, offset, len);
} else if (types[j] == int.class) {
int[] ia = (int[]) data[j];
bf.format(ia[i], buffer, offset, len);
} else if (types[j] == float.class) {
float[] fa = (float[]) data[j];
bf.format(fa[i], buffer, offset, len);
} else if (types[j] == double.class) {
double[] da = (double[]) data[j];
bf.format(da[i], buffer, offset, len);
} else if (types[j] == long.class) {
long[] la = (long[]) data[j];
bf.format(la[i], buffer, offset, len);
}
}
} catch (TruncationException e) {
System.err.println("Ignoring truncation error:" + i
+ "," + j);
}
}
}
}
// Now write the buffer.
try {
str.write(buffer);
byte[] padding = new byte[FitsUtil.padding(buffer.length)];
for (int i = 0; i < padding.length; i += 1) {
padding[i] = (byte) ' ';
}
if (buffer.length > 0) {
str.write(padding);
}
str.flush();
} catch (IOException e) {
throw new FitsException("Error writing ASCII Table data");
}
}