SimpleIcsWriter.java revision 4577f71f76c94dc9fcb06efd2656970925dd3f6a
1/* Copyright 2010, The Android Open Source Project
2 **
3 ** Licensed under the Apache License, Version 2.0 (the "License");
4 ** you may not use this file except in compliance with the License.
5 ** You may obtain a copy of the License at
6 **
7 **     http://www.apache.org/licenses/LICENSE-2.0
8 **
9 ** Unless required by applicable law or agreed to in writing, software
10 ** distributed under the License is distributed on an "AS IS" BASIS,
11 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 ** See the License for the specific language governing permissions and
13 ** limitations under the License.
14 */
15
16package com.android.exchange.utility;
17
18import java.io.CharArrayWriter;
19import java.io.IOException;
20
21public class SimpleIcsWriter extends CharArrayWriter {
22    public static final int MAX_LINE_LENGTH = 75;
23    public static final int LINE_BREAK_LENGTH = 3;
24    public static final String LINE_BREAK = "\r\n\t";
25    int mLineCount = 0;
26
27    public SimpleIcsWriter() {
28        super();
29    }
30
31    private void newLine() {
32        write('\r');
33        write('\n');
34        mLineCount = 0;
35    }
36
37    @Override
38    public void write(String str) throws IOException {
39        int len = str.length();
40        // Handle the simple case here to avoid unnecessary looping
41        if (mLineCount + len < MAX_LINE_LENGTH) {
42            mLineCount += len;
43            super.write(str);
44            return;
45        }
46        for (int i = 0; i < len; i++, mLineCount++) {
47            if (mLineCount == MAX_LINE_LENGTH) {
48                write('\r');
49                write('\n');
50                write('\t');
51                mLineCount = 0;
52            }
53            write(str.charAt(i));
54        }
55    }
56
57    public void writeTag(String name, String value) throws IOException {
58        // Belt and suspenders here; don't crash on null value.  Use something innocuous
59        if (value == null) value = "0";
60        write(name);
61        write(":");
62        write(value);
63        newLine();
64    }
65}
66