/*
 * This file is part of libbluray
 * Copyright (C) 2010  William Hahne
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package org.videolan;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.videolan.Logger;

/**
 * This class handle mounting jar files so that their contents can be accessed.
 *
 * @author William Hahne
 *
 */
public class MountManager {
    public static String mount(int jarId) throws MountException {
        String jarStr = jarIdToString(jarId);

        logger.info("Mounting JAR: " + jarStr);

        if (jarStr == null)
            throw new IllegalArgumentException();

        String path = System.getProperty("bluray.vfs.root") + "/BDMV/JAR/" + jarStr + ".jar";

        JarFile jar = null;
        File tmpDir = null;
        try {
            jar = new JarFile(path);
            tmpDir = File.createTempFile("bdj-", "");
        } catch (IOException e) {
            e.printStackTrace();
            throw new MountException();
        }

        // create temporary directory
        tmpDir.delete();
        tmpDir.mkdir();

        try {
            Enumeration entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = (JarEntry)entries.nextElement();
                File out = new File(tmpDir + File.separator + entry.getName());

                if (entry.isDirectory()) {
                    out.mkdirs();
                } else {
                    /* make sure path exists */
                    out.getParentFile().mkdirs();

                    InputStream inStream = jar.getInputStream(entry);
                    OutputStream outStream = new FileOutputStream(out);

                    while (inStream.available() > 0) {
                        outStream.write(inStream.read());
                    }

                    inStream.close();
                    outStream.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            recursiveDelete(tmpDir);
            throw new MountException();
        }

        mountPoints.put(new Integer(jarId), tmpDir);
        return tmpDir.getAbsolutePath();
    }

    public static void unmount(int jarId) {
        logger.info("Unmounting JAR: " + jarId);
        Integer id = new Integer(jarId);
        File mountPoint = (File)mountPoints.get(id);
        if (mountPoint != null) {
            recursiveDelete(mountPoint);
            mountPoints.remove(id);
        }
    }

    public static void unmountAll() {
        Iterator iterator = mountPoints.keySet().iterator();
        while (iterator.hasNext())
            unmount(((Integer)iterator.next()).intValue());
    }

    public static String getMount(int jarId) {
        Integer id = new Integer(jarId);
        if (mountPoints.containsKey(id)) {
            return ((File)mountPoints.get(id)).getAbsolutePath();
        } else {
            return null;
        }
    }

    private static String jarIdToString(int jarId) {
        if (jarId < 0 || jarId > 99999)
            return null;
        return BDJUtil.makeFiveDigitStr(jarId);
    }

    private static void recursiveDelete(File dir) {
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                recursiveDelete(file);
            } else {
                file.delete();
            }
        }

        dir.delete();
    }

    private static Map mountPoints = Collections.synchronizedMap(new HashMap());
    private static final Logger logger = Logger.getLogger(MountManager.class.getName());
}

@@ -1517,24 +1517,199 @@
 ception();%0A%0A
+        String oldPath = getMount(jarId);%0A        if (oldPath != null) %7B%0A            logger.error(%22JAR %22 + jarId + %22 already mounted%22);%0A            return oldPath;%0A        %7D%0A%0A
         Stri
@@ -1790,16 +1790,16 @@
 %22.jar%22;%0A
-
 %0A       
@@ -2164,32 +2164,79 @@
 %0A%0A        try %7B%0A
+            byte%5B%5D buffer = new byte%5B32*1024%5D;%0A
             Enum
@@ -2459,24 +2459,86 @@
 etName());%0A%0A
+                logger.info(%22   mount: %22 + entry.getName());%0A%0A
             
@@ -2893,34 +2893,78 @@
     
-while (inStream.available(
+int length;%0A                    while ((length = inStream.read(buffer)
 ) %3E 
@@ -3012,23 +3012,25 @@
 ite(
-inStream.read()
+buffer, 0, length
 );%0A 
@@ -3296,32 +3296,32 @@
 untException();%0A
-
         %7D%0A%0A     
@@ -3307,32 +3307,94 @@
 n();%0A        %7D%0A%0A
+        logger.info(%22Mounting JAR %22 + jarId + %22 complete.%22);%0A%0A
         mountPoi

Improve mounting .jar files.