public void writeValue(DataOutput out, Object value) throws IOException { int leng = Math.min(value == null ? 0 : Array.getLength(value), nel_); for (int i = 0; i < leng; i++) { arrayWriter_.writeElement(out, value, i); } for (int i = leng; i < nel_; i++) { arrayWriter_.writePad(out); } }
public double getZero() { return arrayWriter_.getZero(); }
public int getLength() { return nel_ * arrayWriter_.getByteCount(); }
public char getFormatChar() { return arrayWriter_.getFormatChar(); }
/** * Returns a column writer capable of writing a given column to a stream in FITS format. * * @param cinfo describes the column to write * @param maxShape shape for array values * @param varShape whether shapes are variable * @param elementSize element size * @param maxEls maximum number of elements for any array in column (only applies if varShape is * true) * @param totalEls total number of elements for all array values in col (only applies if varShape * is true) * @param nullableInt true if we are going to have to store nulls in an integer column * @return a suitable column writer, or <tt>null</tt> if we don't know how to write this to FITS */ ColumnWriter createColumnWriter( ColumnInfo cinfo, int[] shape, boolean varShape, int eSize, final int maxEls, long totalEls, boolean nullableInt) { Class clazz = cinfo.getContentClass(); if (clazz == String.class) { final int maxChars = eSize; final int[] dims = new int[] {maxChars}; final byte[] buf = new byte[maxChars]; final byte[] blankBuf = new byte[maxChars]; final byte padByte = (byte) ' '; Arrays.fill(blankBuf, padByte); return new ColumnWriter() { public void writeValue(DataOutput out, Object value) throws IOException { final byte[] bytes; if (value == null) { bytes = blankBuf; } else { bytes = buf; String sval = (String) value; int leng = Math.min(sval.length(), maxChars); for (int i = 0; i < leng; i++) { bytes[i] = (byte) sval.charAt(i); } for (int i = leng; i < maxChars; i++) { bytes[i] = padByte; } } out.write(bytes); } public String getFormat() { return Integer.toString(maxChars) + 'A'; } public char getFormatChar() { return 'A'; } public int getLength() { return maxChars; } public int[] getDims() { return dims; } public double getZero() { return 0.0; } public double getScale() { return 1.0; } public Number getBadNumber() { return null; } }; } else if (clazz == String[].class) { final int maxChars = eSize; final int[] charDims = new int[shape.length + 1]; charDims[0] = maxChars; System.arraycopy(shape, 0, charDims, 1, shape.length); final byte[] buf = new byte[maxChars]; final byte padByte = (byte) ' '; return new ColumnWriter() { public void writeValue(DataOutput out, Object value) throws IOException { int is = 0; if (value != null) { String[] svals = (String[]) value; int leng = Math.min(svals.length, maxEls); for (; is < leng; is++) { String str = svals[is]; int ic = 0; if (str != null) { int sleng = Math.min(str.length(), maxChars); for (; ic < sleng; ic++) { buf[ic] = (byte) str.charAt(ic); } } Arrays.fill(buf, ic, maxChars, padByte); out.write(buf); } } if (is < maxEls) { Arrays.fill(buf, padByte); for (; is < maxEls; is++) { out.write(buf); } } } public String getFormat() { return Integer.toString(maxChars * maxEls) + 'A'; } public char getFormatChar() { return 'A'; } public int getLength() { return maxChars * maxEls; } public int[] getDims() { return charDims; } public double getZero() { return 0.0; } public double getScale() { return 1.0; } public Number getBadNumber() { return null; } }; } else { ColumnWriter cw = ScalarColumnWriter.createColumnWriter(cinfo, nullableInt, allowSignedByte); if (cw != null) { return cw; } else { ArrayWriter aw = ArrayWriter.createArrayWriter(cinfo, allowSignedByte); if (aw != null) { return new FixedArrayColumnWriter(aw, shape); } else { return null; } } } }