| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.apache.commons.io.input; |
| 18 |
|
| 19 |
import static org.apache.commons.io.IOUtils.CR; |
| 20 |
import static org.apache.commons.io.IOUtils.EOF; |
| 21 |
import static org.apache.commons.io.IOUtils.LF; |
| 22 |
|
| 23 |
import java.io.IOException; |
| 24 |
import java.io.InputStream; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class UnixLineEndingInputStream extends InputStream { |
| 32 |
|
| 33 |
private boolean atEos; |
| 34 |
|
| 35 |
private boolean atSlashCr; |
| 36 |
|
| 37 |
private boolean atSlashLf; |
| 38 |
|
| 39 |
private final InputStream in; |
| 40 |
|
| 41 |
private final boolean lineFeedAtEndOfFile; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public UnixLineEndingInputStream(final InputStream inputStream, final boolean ensureLineFeedAtEndOfFile) { |
| 50 |
this.in = inputStream; |
| 51 |
this.lineFeedAtEndOfFile = ensureLineFeedAtEndOfFile; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@Override |
| 59 |
public void close() throws IOException { |
| 60 |
super.close(); |
| 61 |
in.close(); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private int handleEos(final boolean previousWasSlashCr) { |
| 71 |
if (previousWasSlashCr || !lineFeedAtEndOfFile) { |
| 72 |
return EOF; |
| 73 |
} |
| 74 |
if (!atSlashLf) { |
| 75 |
atSlashLf = true; |
| 76 |
return LF; |
| 77 |
} |
| 78 |
return EOF; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@Override |
| 85 |
public synchronized void mark(final int readLimit) { |
| 86 |
throw UnsupportedOperationExceptions.mark(); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
@Override |
| 93 |
public synchronized int read() throws IOException { |
| 94 |
final boolean previousWasSlashR = atSlashCr; |
| 95 |
if (atEos) { |
| 96 |
return handleEos(previousWasSlashR); |
| 97 |
} |
| 98 |
final int target = readWithUpdate(); |
| 99 |
if (atEos) { |
| 100 |
return handleEos(previousWasSlashR); |
| 101 |
} |
| 102 |
if (atSlashCr) { |
| 103 |
return LF; |
| 104 |
} |
| 105 |
|
| 106 |
if (previousWasSlashR && atSlashLf) { |
| 107 |
return read(); |
| 108 |
} |
| 109 |
|
| 110 |
return target; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private int readWithUpdate() throws IOException { |
| 119 |
final int target = this.in.read(); |
| 120 |
atEos = target == EOF; |
| 121 |
if (atEos) { |
| 122 |
return target; |
| 123 |
} |
| 124 |
atSlashCr = target == CR; |
| 125 |
atSlashLf = target == LF; |
| 126 |
return target; |
| 127 |
} |
| 128 |
} |