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

br.com.objectos.way.base.zip.WayZipEntry Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.base.zip;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.io.ByteStreams;

/**
 * @author [email protected] (Marcio Endo)
 */
public abstract class WayZipEntry {

  public static final Predicate IS_VALID = WayZipEntryIsValid.INSTANCE;
  public static final Predicate IS_NOT_VALID = Predicates.not(IS_VALID);

  public static final Function TO_STREAM = WayZipEntryToStream.INSTANCE;

  public static final Comparator ORDER_BY_NAME = WayZipEntryOrderByName.INSTANCE;

  private final String name;

  WayZipEntry(String name) {
    this.name = name;
  }

  public final String getName() {
    return name;
  }

  public IOException getIOException() {
    throw new UnsupportedOperationException();
  }

  public boolean isValid() {
    return true;
  }

  public abstract InputStream open();

  static WayZipEntry entryOf(ZipFile file, ZipEntry entry) {
    String name = entry.getName();

    try {
      InputStream stream = file.getInputStream(entry);
      byte[] bytes = ByteStreams.toByteArray(stream);
      return new Valid(name, bytes);

    } catch (IOException e) {
      return new Invalid(name, e);

    }
  }

  private static class Valid extends WayZipEntry {

    private final byte[] bytes;

    public Valid(String name, byte[] bytes) {
      super(name);
      this.bytes = bytes;
    }

    @Override
    public InputStream open() {
      return new ByteArrayInputStream(bytes);
    }

  }

  private static class Invalid extends WayZipEntry {

    private final IOException exception;

    public Invalid(String name, IOException exception) {
      super(name);
      this.exception = exception;
    }

    @Override
    public IOException getIOException() {
      return exception;
    }

    @Override
    public boolean isValid() {
      return false;
    }

    @Override
    public InputStream open() {
      throw new UnsupportedOperationException();
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy