Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Refactor to use InputStream instead of byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
quinarygio committed Jul 31, 2017
1 parent a6a9613 commit c5b5182
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package eu.itesla_project.contingency;

import java.io.InputStream;
import java.nio.file.Path;

/**
Expand All @@ -19,7 +20,7 @@ default <T extends ContingenciesProvider> T create(Path contingenciesFile) {
return create();
}

default <T extends ContingenciesProvider> T create(byte[] data) {
default <T extends ContingenciesProvider> T create(InputStream data) {
return create();
}

Expand Down
4 changes: 4 additions & 0 deletions iidm/iidm-converter-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>net.java.truevfs</groupId>
<artifactId>truevfs-driver-zip</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,30 @@
package eu.itesla_project.iidm.datasource;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;

/**
* @author Giovanni Ferrari <giovanni.ferrari@techrain.it>
*/
public class Bzip2MemDataSource extends ReadOnlyMemDataSource {

public Bzip2MemDataSource() {
super();
public Bzip2MemDataSource(String baseName) {
super(baseName);
}

public Bzip2MemDataSource(byte[] content, String filename) {
super();
Objects.requireNonNull(filename);

public Bzip2MemDataSource(InputStream content, String filename) {
super(DataSourceUtil.getBaseName(filename));
String zipped = filename.substring(0,filename.lastIndexOf("."));
String format = zipped.substring(zipped.lastIndexOf(".")+1);
putData(null, format, content);
try{
putData(zipped, content);
}
catch(IOException ie)
{
throw new RuntimeException(ie);
}
}

protected String getCompressionExt() {
Expand All @@ -45,7 +43,7 @@ protected InputStream getCompressedInputStream(InputStream is) throws IOExceptio

@Override
public InputStream newInputStream(String suffix, String ext) throws IOException {
return getCompressedInputStream(super.newInputStream(suffix, ext));
return newInputStream(DataSourceUtil.getFileName(getBaseName(), suffix, ext));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package eu.itesla_project.iidm.datasource;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand All @@ -20,9 +22,15 @@ public interface DataSourceUtil {
OpenOption[] APPEND_OPEN_OPTIONS = { StandardOpenOption.APPEND };

static String getFileName(String baseName, String suffix, String ext) {
Objects.requireNonNull(baseName);
return baseName + (suffix != null ? suffix : "") + (ext != null ? "." + ext : "");
}

static String getCompressedFileName(String baseName, String suffix, String format, String ext) {
Objects.requireNonNull(baseName);
return baseName + (suffix != null ? suffix : "") + (format != null ? "." + format : "") + (ext != null ? "." + ext : "");
}

static OpenOption[] getOpenOptions(boolean append) {
return append ? APPEND_OPEN_OPTIONS : DEFAULT_OPEN_OPTIONS;
}
Expand All @@ -32,6 +40,7 @@ static String getBaseName(Path file) {
}

static String getBaseName(String fileName) {
Objects.requireNonNull(fileName);
int pos = fileName.indexOf('.'); // find first dot in case of double extension (.xml.gz)
return pos == -1 ? fileName : fileName.substring(0, pos);
}
Expand All @@ -50,42 +59,40 @@ static DataSource createDataSource(Path directory, String fileNameOrBaseName, Da
return new FileDataSource(directory, getBaseName(fileNameOrBaseName), observer);
}
}
static ReadOnlyMemDataSource createMemDataSource(byte[] data, String extension, String format) {

static ReadOnlyMemDataSource createMemDataSource(InputStream data, String basename, String extension, String format) throws IOException{
Objects.requireNonNull(data);
Objects.requireNonNull(extension);
ReadOnlyMemDataSource mem = null;
if (extension.equalsIgnoreCase("zip")) {
mem = new ZipMemDataSource();
} else
mem = new ZipMemDataSource(data, basename);
} else
if (extension.equalsIgnoreCase("gz")) {
mem = new GzMemDataSource();
mem = new GzMemDataSource(data, getCompressedFileName(basename, null, format, extension));
} else if (extension.equalsIgnoreCase("bz2")) {
mem = new Bzip2MemDataSource();
mem = new Bzip2MemDataSource(data, getCompressedFileName(basename, null, format, extension));
} else {
mem = new MemDataSource();
mem.putData(getFileName(basename, null, format), data);
}
mem.putData(null, format, data);
return mem;
}
static ReadOnlyMemDataSource createMemDataSource(byte[] data, String filename) {

static ReadOnlyMemDataSource createMemDataSource(InputStream data, String filename) throws IOException{
Objects.requireNonNull(data);
Objects.requireNonNull(filename);
ReadOnlyMemDataSource mem = null;
if (filename.endsWith(".zip")) {
mem = new ZipMemDataSource(data);
} else
mem = new ZipMemDataSource(data, getBaseName(filename));
} else
if (filename.endsWith(".gz")) {
mem = new GzMemDataSource(data, filename);
} else if (filename.endsWith(".bz2")) {
mem = new Bzip2MemDataSource(data, filename);
} else {
mem = new ReadOnlyMemDataSource();
String format = filename.substring(filename.lastIndexOf(".")+1);
mem.putData(null, format, data);
mem = new ReadOnlyMemDataSource(getBaseName(filename));
mem.putData(filename, data);
}
return mem;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ public class GzMemDataSource extends ReadOnlyMemDataSource {
public GzMemDataSource() {
super();
}

public GzMemDataSource(byte[] content, String filename) {
super();
Objects.requireNonNull(filename);

public GzMemDataSource(InputStream content, String filename) {
super(DataSourceUtil.getBaseName(filename));
String zipped = filename.substring(0,filename.lastIndexOf("."));
String format = zipped.substring(zipped.lastIndexOf(".")+1);
putData(null, format, content);
try{
putData(zipped, content);
}
catch(IOException ie)
{
throw new RuntimeException(ie);
}
}

protected String getCompressionExt() {
Expand All @@ -40,10 +44,9 @@ protected InputStream getCompressedInputStream(InputStream is) throws IOExceptio
return new GZIPInputStream(is);
}


@Override
public InputStream newInputStream(String suffix, String ext) throws IOException {
return getCompressedInputStream(super.newInputStream(suffix, ext));
return newInputStream(DataSourceUtil.getFileName(getBaseName(), suffix, ext));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,35 @@
*/
package eu.itesla_project.iidm.datasource;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class MemDataSource extends ReadOnlyMemDataSource implements DataSource{


@Override
public OutputStream newOutputStream(final String suffix, final String ext, boolean append) throws IOException {
final Key key = new Key(suffix, ext);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
if (append) {
byte[] ba = data.get(new Key(suffix, ext));
if (ba != null) {
os.write(ba, 0, ba.length);
}
}
return new ObservableOutputStream(os, key.toString(), new AbstractDataSourceObserver() {
@Override
public void closed(String streamName) {
data.put(key, os.toByteArray());
}
});
return newOutputStream(DataSourceUtil.getFileName("", suffix, ext), append);
}

@Override
public OutputStream newOutputStream(String fileName, boolean append) throws IOException {
Objects.requireNonNull(fileName);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
if (append) {
byte[] ba = data2.get(fileName);
byte[] ba = getData(fileName);
if (ba != null) {
os.write(ba, 0, ba.length);
}
}
return new ObservableOutputStream(os, fileName, new AbstractDataSourceObserver() {
@Override
public void closed(String streamName) {
data2.put(fileName, os.toByteArray());
putData(fileName, os.toByteArray());
}
});
}
Expand Down
Loading

0 comments on commit c5b5182

Please sign in to comment.