
com.day.jcr.vault.util.LineInputStream Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2011 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*
**************************************************************************/
package com.day.jcr.vault.util;
import java.io.IOException;
import java.io.InputStream;
/**
* Provides an input stream wrapper that detects line feed sequences and
* replaces them by new ones.
*
*/
public class LineInputStream extends InputStream {
public static final byte[] LS_UNIX = new byte[]{0x0a};
public static final byte[] LS_WINDOWS = new byte[]{0x0d, 0x0a};
public static final byte[] LS_NATIVE = System.getProperty("line.separator").getBytes();
private byte[] buffer = new byte[8192];
private byte[] lineFeed = LS_NATIVE;
private byte[] lineSpool;
private int pos = 0;
private int end = 0;
private static final char STATE_INIT = ' ';
private static final char STATE_CR = 'c';
private static final char STATE_LF = 'l';
private static final char STATE_CRLF = 'f';
private char state = STATE_INIT;
boolean isEof = false;
private byte[] spool;
private int spoolPos = 0;
private final InputStream in;
public LineInputStream(InputStream in, byte[] ls) {
this.in = in;
if (ls != null) {
lineFeed = ls;
}
lineSpool = new byte[lineFeed.length + 1];
System.arraycopy(lineFeed, 0, lineSpool, 0, lineFeed.length);
}
private int fillBuffer() throws IOException {
int ret = in.read(buffer, end, buffer.length - end);
if (ret >= 0) {
end += ret;
} else {
isEof = true;
}
return ret;
}
public int read() throws IOException {
final byte[] one = new byte[1];
if (read(one) == -1) {
return -1;
}
return one[0];
}
public int read(byte b[], int off, int len) throws IOException {
if (isEof) {
if (spool == null) {
if (state != STATE_INIT) {
spool = lineFeed;
state = STATE_INIT;
} else {
return -1;
}
}
}
int total = 0;
while (total < len) {
if (spool != null) {
b[off+(total++)] = spool[spoolPos++];
if (spoolPos == spool.length) {
spool = null;
spoolPos = 0;
}
} else {
if (pos == end) {
int ret = fillBuffer();
if (ret == 0 && pos == end) {
// in this case we didn't get more, so flush
pos = end = 0;
continue;
} else if (ret == -1) {
break;
}
}
byte c = buffer[pos++];
if (c == 0x0a) {
switch (state) {
case STATE_INIT:
state = STATE_LF;
break;
case STATE_CR:
state = STATE_CRLF;
break;
case STATE_LF:
spool = lineFeed;
break;
case STATE_CRLF:
spool = lineFeed;
state = STATE_LF;
}
} else if (c == 0x0d) {
switch (state) {
case STATE_INIT:
state = STATE_CR;
break;
case STATE_LF:
state = STATE_CRLF;
break;
case STATE_CR:
spool = lineFeed;
break;
case STATE_CRLF:
spool = lineFeed;
state = STATE_CR;
}
} else {
if (state != STATE_INIT) {
spool = lineSpool;
lineSpool[lineSpool.length - 1] = c;
state = STATE_INIT;
} else {
b[off + (total++)] = c;
}
}
}
}
return total;
}
public void close() throws IOException {
in.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy