1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.voicemail.impl.mail.utility;
17 
18 import java.io.FilterOutputStream;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 
22 public class EOLConvertingOutputStream extends FilterOutputStream {
23   int lastChar;
24 
EOLConvertingOutputStream(OutputStream out)25   public EOLConvertingOutputStream(OutputStream out) {
26     super(out);
27   }
28 
29   @Override
write(int oneByte)30   public void write(int oneByte) throws IOException {
31     if (oneByte == '\n') {
32       if (lastChar != '\r') {
33         super.write('\r');
34       }
35     }
36     super.write(oneByte);
37     lastChar = oneByte;
38   }
39 
40   @Override
flush()41   public void flush() throws IOException {
42     if (lastChar == '\r') {
43       super.write('\n');
44       lastChar = '\n';
45     }
46     super.flush();
47   }
48 }
49