| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.net.io.ToNetASCIIOutputStream |
|
|
| 1 | /* |
|
| 2 | * Copyright 2001-2005 The Apache Software Foundation |
|
| 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 org.apache.commons.net.io; |
|
| 17 | ||
| 18 | import java.io.FilterOutputStream; |
|
| 19 | import java.io.IOException; |
|
| 20 | import java.io.OutputStream; |
|
| 21 | ||
| 22 | /*** |
|
| 23 | * This class wraps an output stream, replacing all singly occurring |
|
| 24 | * <LF> (linefeed) characters with <CR><LF> (carriage return |
|
| 25 | * followed by linefeed), which is the NETASCII standard for representing |
|
| 26 | * a newline. |
|
| 27 | * You would use this class to implement ASCII file transfers requiring |
|
| 28 | * conversion to NETASCII. |
|
| 29 | * <p> |
|
| 30 | * <p> |
|
| 31 | * @author Daniel F. Savarese |
|
| 32 | ***/ |
|
| 33 | ||
| 34 | public final class ToNetASCIIOutputStream extends FilterOutputStream |
|
| 35 | { |
|
| 36 | private boolean __lastWasCR; |
|
| 37 | ||
| 38 | /*** |
|
| 39 | * Creates a ToNetASCIIOutputStream instance that wraps an existing |
|
| 40 | * OutputStream. |
|
| 41 | * <p> |
|
| 42 | * @param output The OutputStream to wrap. |
|
| 43 | ***/ |
|
| 44 | public ToNetASCIIOutputStream(OutputStream output) |
|
| 45 | { |
|
| 46 | 64 | super(output); |
| 47 | 64 | __lastWasCR = false; |
| 48 | 64 | } |
| 49 | ||
| 50 | ||
| 51 | /*** |
|
| 52 | * Writes a byte to the stream. Note that a call to this method |
|
| 53 | * may result in multiple writes to the underlying input stream in order |
|
| 54 | * to convert naked newlines to NETASCII line separators. |
|
| 55 | * This is transparent to the programmer and is only mentioned for |
|
| 56 | * completeness. |
|
| 57 | * <p> |
|
| 58 | * @param ch The byte to write. |
|
| 59 | * @exception IOException If an error occurs while writing to the underlying |
|
| 60 | * stream. |
|
| 61 | ***/ |
|
| 62 | public synchronized void write(int ch) |
|
| 63 | throws IOException |
|
| 64 | { |
|
| 65 | 20 | switch (ch) |
| 66 | { |
|
| 67 | case '\r': |
|
| 68 | 0 | __lastWasCR = true; |
| 69 | 0 | out.write('\r'); |
| 70 | 0 | return ; |
| 71 | case '\n': |
|
| 72 | 0 | if (!__lastWasCR) |
| 73 | 0 | out.write('\r'); |
| 74 | // Fall through |
|
| 75 | default: |
|
| 76 | 20 | __lastWasCR = false; |
| 77 | 20 | out.write(ch); |
| 78 | 20 | return ; |
| 79 | } |
|
| 80 | } |
|
| 81 | ||
| 82 | ||
| 83 | /*** |
|
| 84 | * Writes a byte array to the stream. |
|
| 85 | * <p> |
|
| 86 | * @param buffer The byte array to write. |
|
| 87 | * @exception IOException If an error occurs while writing to the underlying |
|
| 88 | * stream. |
|
| 89 | ***/ |
|
| 90 | public synchronized void write(byte buffer[]) |
|
| 91 | throws IOException |
|
| 92 | { |
|
| 93 | 4 | write(buffer, 0, buffer.length); |
| 94 | 4 | } |
| 95 | ||
| 96 | ||
| 97 | /*** |
|
| 98 | * Writes a number of bytes from a byte array to the stream starting from |
|
| 99 | * a given offset. |
|
| 100 | * <p> |
|
| 101 | * @param buffer The byte array to write. |
|
| 102 | * @param offset The offset into the array at which to start copying data. |
|
| 103 | * @param length The number of bytes to write. |
|
| 104 | * @exception IOException If an error occurs while writing to the underlying |
|
| 105 | * stream. |
|
| 106 | ***/ |
|
| 107 | public synchronized void write(byte buffer[], int offset, class="keyword">int length) |
|
| 108 | throws IOException |
|
| 109 | { |
|
| 110 | 24 | while (length-- > 0) |
| 111 | 20 | write(buffer[offset++]); |
| 112 | 4 | } |
| 113 | ||
| 114 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |