Skip to content

Commit

Permalink
share temp file code
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Oct 5, 2018
1 parent 94894fd commit f48ac4f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 49 deletions.
16 changes: 1 addition & 15 deletions src/main/java/com/monitorjbl/xlsx/StreamingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand All @@ -37,6 +36,7 @@

import static com.monitorjbl.xlsx.XmlUtils.document;
import static com.monitorjbl.xlsx.XmlUtils.searchForNodeList;
import static com.monitorjbl.xlsx.impl.TempFileUtil.writeInputStreamToFile;

/**
* Streaming Excel workbook implementation. Most advanced features of POI are not supported.
Expand Down Expand Up @@ -86,20 +86,6 @@ public void close() throws IOException {
}
}

static File writeInputStreamToFile(InputStream is, int bufferSize) throws IOException {
File f = Files.createTempFile("tmp-", ".xlsx").toFile();
try(FileOutputStream fos = new FileOutputStream(f)) {
int read;
byte[] bytes = new byte[bufferSize];
while((read = is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
is.close();
fos.close();
return f;
}
}

public static Builder builder() {
return new Builder();
}
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -395,20 +390,6 @@ public void close() {
}
}

static File writeInputStreamToFile(InputStream is, int bufferSize) throws IOException {
File f = Files.createTempFile("tmp-", ".xlsx").toFile();
try(FileOutputStream fos = new FileOutputStream(f)) {
int read;
byte[] bytes = new byte[bufferSize];
while((read = is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
is.close();
fos.close();
return f;
}
}

class StreamingRowIterator implements Iterator<Row> {
public StreamingRowIterator() {
if(rowCacheIterator == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -39,6 +38,7 @@

import static com.monitorjbl.xlsx.XmlUtils.document;
import static com.monitorjbl.xlsx.XmlUtils.searchForNodeList;
import static com.monitorjbl.xlsx.impl.TempFileUtil.writeInputStreamToFile;
import static java.util.Arrays.asList;

public class StreamingWorkbookReader implements Iterable<Sheet>, AutoCloseable {
Expand Down Expand Up @@ -211,20 +211,6 @@ public void close() throws IOException {
}
}

static File writeInputStreamToFile(InputStream is, int bufferSize) throws IOException {
File f = Files.createTempFile("tmp-", ".xlsx").toFile();
try(FileOutputStream fos = new FileOutputStream(f)) {
int read;
byte[] bytes = new byte[bufferSize];
while((read = is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
is.close();
fos.close();
return f;
}
}

static class StreamingSheetIterator implements Iterator<Sheet> {
private final Iterator<StreamingSheet> iterator;

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/monitorjbl/xlsx/impl/TempFileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.monitorjbl.xlsx.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

public class TempFileUtil {
public static File writeInputStreamToFile(InputStream is, int bufferSize) throws IOException {
File f = Files.createTempFile("tmp-", ".xlsx").toFile();
try(FileOutputStream fos = new FileOutputStream(f)) {
int read;
byte[] bytes = new byte[bufferSize];
while((read = is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
return f;
} finally {
is.close();
}
}
}

0 comments on commit f48ac4f

Please sign in to comment.