Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class EgovFlatFileByteReader<T> extends AbstractItemCountingItemStreamIte
private static final Logger LOGGER = LoggerFactory.getLogger(EgovFlatFileByteReader.class);
private static final int UNIX_CRLF = 1;
private static final int WINDOWS_CRLF = 2;
public static int LINE_CRLF = 2;
private int lineCrlf = WINDOWS_CRLF;
byte[] b = null;
private RecordSeparatorPolicy recordSeparatorPolicy = new SimpleRecordSeparatorPolicy();
private EgovByteReaderFactory bufferedReaderFactory = new EgovByteReaderFactory();
Expand Down Expand Up @@ -112,12 +112,19 @@ public void setLength(int length) {
*/
public void setOsType(String osType) {
if (!osType.equalsIgnoreCase("WINDOWS")) {
LINE_CRLF = UNIX_CRLF;
this.lineCrlf = UNIX_CRLF;
} else {
LINE_CRLF = WINDOWS_CRLF;
this.lineCrlf = WINDOWS_CRLF;
}
}

/**
* 라인 구분자 길이를 반환
*/
public int getLineCrlf() {
return this.lineCrlf;
}

/**
* Factory for the {@link BufferedReader} that will be used to extract lines
* from the file. The default is fine for plain text files, but this is a
Expand Down Expand Up @@ -186,13 +193,13 @@ protected T doRead() throws Exception {
*/
private byte[] readLine() {
if (b == null) {
b = new byte[length + LINE_CRLF];
b = new byte[length + this.lineCrlf];
}

int line = 0;

try {
line = this.reader.read(b, offset, length + LINE_CRLF);
line = this.reader.read(b, offset, length + this.lineCrlf);
if (line < 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.egovframe.rte.bat.core.item.file.transform;

import org.egovframe.rte.bat.core.item.file.EgovFlatFileByteReader;
import org.springframework.batch.item.file.transform.IncorrectLineLengthException;
import org.springframework.batch.item.file.transform.Range;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -66,6 +65,14 @@ public class EgovFixedByteTokenizer extends EgovAbstractByteLineTokenizer {
*/
private String byteEncoding = DEFAULT_CHARSET;

private static final int UNIX_CRLF = 1;
private static final int WINDOWS_CRLF = 2;

/**
* 라인 구분자 길이
*/
private int lineCrlf = WINDOWS_CRLF;

/**
* 범위값을 세팅
*/
Expand All @@ -81,6 +88,24 @@ public void setByteEncoding(String encoding) {
this.byteEncoding = encoding;
}

/**
* 라인 구분자 길이를 세팅
*/
public void setLineCrlf(int lineCrlf) {
this.lineCrlf = lineCrlf;
}

/**
* OS타입에 따라 라인 구분자 길이를 세팅
*/
public void setOsType(String osType) {
if (!osType.equalsIgnoreCase("WINDOWS")) {
this.lineCrlf = UNIX_CRLF;
} else {
this.lineCrlf = WINDOWS_CRLF;
}
}

/**
* 주어진 구간값을 계산하여 범위를 산정
*/
Expand Down Expand Up @@ -130,7 +155,7 @@ protected List<String> doTokenize(byte[] line) throws Exception {
protected List<String> doTokenize(byte[] byteString, String encoding) throws Exception {
String token;
List<String> tokens = new ArrayList<String>(ranges.length);
int lineLength = byteString.length - EgovFlatFileByteReader.LINE_CRLF;
int lineLength = byteString.length - this.lineCrlf;

if (lineLength == 0) {
throw new IncorrectLineLengthException("Line length must be longer than 0", maxRange, lineLength);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.egovframe.rte.bat.core.item.file;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class EgovFlatFileByteReaderTest {

@Test
public void testOsTypeIsIsolatedPerReader() {
EgovFlatFileByteReader<Object> readerA = new EgovFlatFileByteReader<>();
readerA.setOsType("UNIX");
EgovFlatFileByteReader<Object> readerB = new EgovFlatFileByteReader<>();
readerB.setOsType("WINDOWS");

// Regression guard: the old shared static value was overwritten by reader B.
assertEquals(1, readerA.getLineCrlf());
assertEquals(2, readerB.getLineCrlf());
}

@Test
public void testDefaultOsTypeIsWindows() {
EgovFlatFileByteReader<Object> reader = new EgovFlatFileByteReader<>();

assertEquals(2, reader.getLineCrlf());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.egovframe.rte.bat.core.item.file.transform;

import org.junit.jupiter.api.Test;
import org.springframework.batch.item.file.transform.Range;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class EgovFixedByteTokenizerTest {

@Test
public void testDefaultWindowsLineCrlf() throws Exception {
EgovFixedByteTokenizer tokenizer = createTokenizer();
byte[] line = "ABCDEF\r\n".getBytes(StandardCharsets.US_ASCII);

assertEquals(Arrays.asList("ABC", "DEF"), tokenizer.doTokenize(line, StandardCharsets.US_ASCII.name()));
}

@Test
public void testUnixLineCrlf() throws Exception {
EgovFixedByteTokenizer tokenizer = createTokenizer();
tokenizer.setOsType("UNIX");
byte[] line = "ABCDEF\n".getBytes(StandardCharsets.US_ASCII);

assertEquals(Arrays.asList("ABC", "DEF"), tokenizer.doTokenize(line, StandardCharsets.US_ASCII.name()));
}

private EgovFixedByteTokenizer createTokenizer() {
EgovFixedByteTokenizer tokenizer = new EgovFixedByteTokenizer();
tokenizer.setColumns(new Range[]{new Range(1, 3), new Range(4, 6)});
return tokenizer;
}

}