All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hex.genmodel.InMemoryMojoReaderBackend Maven / Gradle / Ivy

There is a newer version: 3.46.0.6
Show newest version
package hex.genmodel;

import java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class InMemoryMojoReaderBackend implements MojoReaderBackend, Closeable {

  private static final Map CLOSED = Collections.unmodifiableMap(new HashMap());

  private Map _mojoContent;

  public InMemoryMojoReaderBackend(Map mojoContent) {
    _mojoContent = mojoContent;
  }

  @Override
  public BufferedReader getTextFile(String filename) throws IOException {
    checkOpen();
    byte[] data = _mojoContent.get(filename);
    if (data == null)
      throw new IOException("MOJO doesn't contain resource " + filename);
    return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
  }

  @Override
  public byte[] getBinaryFile(String filename) throws IOException {
    checkOpen();
    return _mojoContent.get(filename);
  }

  @Override
  public boolean exists(String filename) {
    checkOpen();
    return _mojoContent.containsKey(filename);
  }

  @Override
  public void close() throws IOException {
    _mojoContent = CLOSED;
  }

  private void checkOpen() {
    if (_mojoContent == CLOSED)
      throw new IllegalStateException("ReaderBackend was already closed");
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy