• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  **********************************************************************
3  * Copyright (c) 2006-2007, Google and others.  All Rights Reserved.
4  **********************************************************************
5  * Author: Mark Davis
6  **********************************************************************
7  */
8 package org.unicode.cldr.util;
9 
10 import java.io.IOException;
11 
12 /**
13  * Class that converts strings to bytes and back. The bytes do not have to be
14  * UTF-8, since they are meant to only be used serially. In particular, the only
15  * restriction is that the transition between serialized characters must be discoverable by
16  * looking at either the last byte of the first character or the first byte of the second character.
17  *
18  * @author markdavis
19  *
20  */
21 public abstract class StringByteConverter {
22 
clear()23     public void clear() {
24         // default implementation does nothing
25     }
26 
27     /**
28      * Return the maximum number of bytes per char.
29      *
30      * @return
31      */
getMaxBytesPerChar()32     public abstract int getMaxBytesPerChar();
33 
34     /**
35      * Converts char of source to output. Result may depend on previous context.
36      * Call clear() before first character, and call toBytes(output, bytePosition) after done.
37      *
38      * @param output
39      *            buffer to fill
40      * @return new byte position
41      */
toBytes(char ch, byte[] output, int bytePosition)42     public abstract int toBytes(char ch, byte[] output, int bytePosition);
43 
44     /**
45      * Converts final state, if any, to output. Result may depend on previous context.
46      * Call clear() before first character, and call toBytes(output, bytePosition) after done.
47      *
48      * @param output
49      *            buffer to fill
50      * @return new byte position
51      */
toBytes(byte[] output, int bytePosition)52     public int toBytes(byte[] output, int bytePosition) {
53         return bytePosition; // default implementation does nothing
54     }
55 
56     /**
57      * Read a string from a byte array. The byte array must be well formed; eg the
58      * contents from byteStart to byteLength will not cause errors, and will never
59      * overrun. It will always terminate at byteLength. The results are not
60      * guaranteed to be the same as would be gotten from inverting toBytes -- that
61      * will happen if multiple strings map to the same bytes.
62      *
63      * @param input
64      *            byte array to read from
65      * @param byteStart
66      *            TODO
67      * @param byteLength
68      *            total length of the byte array
69      * @param result
70      *            the result to add on to
71      * @return the result, for chaining.
72      * @throws IOException
73      */
fromBytes(byte[] input, int byteStart, int byteLength, Appendable result)74     public abstract Appendable fromBytes(byte[] input, int byteStart,
75         int byteLength, Appendable result);
76 
77     /**
78      * Write a string to a byte array.
79      *
80      * @param source
81      *            string to write
82      * @param output
83      *            byte array to write into
84      * @param bytePosition
85      *            place in byte array to start
86      * @return new position in byte array
87      */
toBytes(CharSequence source, byte[] output, int bytePosition)88     public int toBytes(CharSequence source, byte[] output, int bytePosition) {
89         for (int i = 0; i < source.length(); ++i) {
90             bytePosition = toBytes(source.charAt(i), output, bytePosition);
91         }
92         toBytes(output, bytePosition); // cleanup
93         return bytePosition;
94     }
95 
toBytes(CharSequence source)96     public byte[] toBytes(CharSequence source) {
97         byte[] buffer = new byte[source.length() * getMaxBytesPerChar()];
98         int len = toBytes(source, buffer, 0);
99         byte[] result = new byte[len];
100         System.arraycopy(buffer, 0, result, 0, len);
101         return result;
102     }
103 }