net.pms.dlna.ZippedEntry.java Source code

Java tutorial

Introduction

Here is the source code for net.pms.dlna.ZippedEntry.java

Source

/*
 * PS3 Media Server, for streaming any medias to your PS3.
 * Copyright (C) 2008  A.Brochard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License only.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package net.pms.dlna;

import net.pms.formats.Format;
import net.pms.util.FileUtil;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZippedEntry extends DLNAResource implements IPushOutput {
    private static final Logger logger = LoggerFactory.getLogger(ZippedEntry.class);
    private File file;
    private String zeName;
    private long length;
    private ZipFile zipFile;

    @Override
    protected String getThumbnailURL() {
        if (getType() == Format.IMAGE || getType() == Format.AUDIO) {
            // no thumbnail support for now for zipped videos
            return null;
        }

        return super.getThumbnailURL();
    }

    public ZippedEntry(File file, String zeName, long length) {
        this.zeName = zeName;
        this.file = file;
        this.length = length;
    }

    public InputStream getInputStream() {
        return null;
    }

    public String getName() {
        return zeName;
    }

    public long length() {
        if (getPlayer() != null && getPlayer().type() != Format.IMAGE) {
            return DLNAMediaInfo.TRANS_SIZE;
        }

        return length;
    }

    public boolean isFolder() {
        return false;
    }

    // XXX unused
    @Deprecated
    public long lastModified() {
        return 0;
    }

    @Override
    public String getSystemName() {
        return FilenameUtils.getBaseName(file.getAbsolutePath()) + "." + FilenameUtils.getExtension(zeName);
    }

    @Override
    public boolean isValid() {
        resolveFormat();
        setSrtFile(FileUtil.isSubtitlesExists(file, null));
        return getFormat() != null;
    }

    @Override
    public boolean isUnderlyingSeekSupported() {
        return length() < MAX_ARCHIVE_SIZE_SEEK;
    }

    @Override
    public void push(final OutputStream out) throws IOException {
        Runnable r = new Runnable() {
            InputStream in = null;

            public void run() {
                try {
                    int n = -1;
                    byte[] data = new byte[65536];
                    zipFile = new ZipFile(file);
                    ZipEntry ze = zipFile.getEntry(zeName);
                    in = zipFile.getInputStream(ze);

                    while ((n = in.read(data)) > -1) {
                        out.write(data, 0, n);
                    }

                    in.close();
                    in = null;
                } catch (Exception e) {
                    logger.error("Unpack error. Possibly harmless.", e);
                } finally {
                    try {
                        if (in != null) {
                            in.close();
                        }
                        zipFile.close();
                        out.close();
                    } catch (IOException e) {
                        logger.debug("Caught exception", e);
                    }
                }
            }
        };

        new Thread(r, "Zip Extractor").start();
    }

    @Override
    protected void resolveOnce() {
        if (getFormat() == null || !getFormat().isVideo()) {
            return;
        }

        boolean found = false;

        if (!found) {
            if (getMedia() == null) {
                setMedia(new DLNAMediaInfo());
            }

            found = !getMedia().isMediaparsed() && !getMedia().isParsing();

            if (getFormat() != null) {
                InputFile input = new InputFile();
                input.setPush(this);
                input.setSize(length());
                getFormat().parse(getMedia(), input, getType());
            }
        }
    }

    @Override
    public InputStream getThumbnailInputStream() throws IOException {
        if (getMedia() != null && getMedia().getThumb() != null) {
            return getMedia().getThumbnailInputStream();
        } else {
            return super.getThumbnailInputStream();
        }
    }
}