1 package uba.db.column.io;
2
3 import java.io.DataOutput;
4 import java.io.IOException;
5
6 import uba.db.column.CharColumnSpecification;
7
8 /***
9 * Escribe en un {@link DataOutput} el valor de una columna tipo CHAR.
10 *
11 * @see uba.db.column.CharColumnSpecification
12 *
13 * @version $Revision: 1.3 $
14 */
15 public class CharColumnWriter extends ColumnWriterBehavior {
16 private int maxChars;
17
18 /***
19 * @param columnSpecification
20 * especificación de la columna a la cual pertenece este reader.
21 * @param out
22 * stream desde donde se escribirán los datos.
23 */
24 public CharColumnWriter(CharColumnSpecification columnSpecification,
25 DataOutput out) {
26 super(columnSpecification, out);
27 this.maxChars = columnSpecification.maxChars();
28 }
29
30 /***
31 * @see uba.db.column.io.ColumnWriterBehavior#writeTo(DataOutput,
32 * java.lang.Object)
33 */
34 public void writeTo(DataOutput out, Object value) throws IOException {
35 String string = (String) value;
36
37 checkStringLength(string);
38 out.writeChars(string);
39 int n = maxChars - string.length();
40 while (n > 0) {
41 out.writeChar(0);
42 n--;
43 }
44 }
45
46 private void checkStringLength(String string) throws ColumnValueTooLongException {
47 if (string.length() > maxChars) {
48 throw new ColumnValueTooLongException();
49 }
50 }
51 }