Skip to content

Commit

Permalink
Fixed issue iryndin#5: Don't load DBF and MEMO files into memory when…
Browse files Browse the repository at this point in the history
… reading it
  • Loading branch information
lifchicker committed Dec 10, 2014
1 parent 06ffd11 commit d99575c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/main/java/net/iryndin/jdbf/reader/DbfReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

public class DbfReader implements Closeable {

private ByteArrayInputStream dbfInputStream;
private InputStream dbfInputStream;
private MemoReader memoReader;
private DbfMetadata metadata;
private byte[] oneRecordBuffer;
private int recordsCounter = 0;
private static final int BUFFER_SIZE = 8192;

public DbfReader(File dbfFile) throws IOException {
this(new FileInputStream(dbfFile));
Expand All @@ -24,12 +25,12 @@ public DbfReader(File dbfFile, File memoFile) throws IOException {
}

public DbfReader(InputStream dbfInputStream) throws IOException {
this.dbfInputStream = new ByteArrayInputStream(IOUtils.toByteArray(dbfInputStream));
this.dbfInputStream = new BufferedInputStream(dbfInputStream, BUFFER_SIZE);
readMetadata();
}

public DbfReader(InputStream dbfInputStream, InputStream memoInputStream) throws IOException {
this.dbfInputStream = new ByteArrayInputStream(IOUtils.toByteArray(dbfInputStream));
this.dbfInputStream = new BufferedInputStream(dbfInputStream, BUFFER_SIZE);
this.memoReader = new MemoReader(memoInputStream);
readMetadata();
}
Expand All @@ -39,6 +40,7 @@ public DbfMetadata getMetadata() {
}

private void readMetadata() throws IOException {
this.dbfInputStream.mark(1024*1024);
metadata = new DbfMetadata();
readHeader();
DbfMetadataUtils.readFields(metadata, dbfInputStream);
Expand Down Expand Up @@ -77,7 +79,7 @@ public void findFirstRecord() throws IOException {
seek(dbfInputStream, metadata.getFullHeaderLength());
}

private void seek(ByteArrayInputStream inputStream, int position) {
private void seek(InputStream inputStream, int position) throws IOException {
inputStream.reset();
inputStream.skip(position);
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/iryndin/jdbf/reader/MemoReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@
*/
public class MemoReader implements Closeable {

private ByteArrayInputStream memoInputStream;
private static final int BUFFER_SIZE = 8192;
private InputStream memoInputStream;
private MemoFileHeader memoHeader;

public MemoReader(File memoFile) throws IOException {
this(new FileInputStream(memoFile));
}

public MemoReader(InputStream inputStream) throws IOException {
this.memoInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
this.memoInputStream = new BufferedInputStream(inputStream, BUFFER_SIZE);
readMetadata();
}

private void readMetadata() throws IOException {
byte[] headerBytes = new byte[JdbfUtils.MEMO_HEADER_LENGTH];
memoInputStream.mark(8192);
memoInputStream.read(headerBytes);
this.memoHeader = MemoFileHeader.create(headerBytes);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/iryndin/jdbf/util/DbfMetadataUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.iryndin.jdbf.core.DbfFileTypeEnum;
import net.iryndin.jdbf.core.DbfMetadata;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -74,7 +74,7 @@ public static Date parseHeaderUpdateDate(byte yearByte, byte monthByte, byte day

}

public static void readFields(DbfMetadata metadata, ByteArrayInputStream inputStream) throws IOException {
public static void readFields(DbfMetadata metadata, InputStream inputStream) throws IOException {
List<DbfField> fields = new ArrayList<>();
byte[] fieldBytes = new byte[JdbfUtils.FIELD_RECORD_LENGTH];
int headerLength = 0;
Expand Down

0 comments on commit d99575c

Please sign in to comment.