// Path: src/main/java/be/bagofwords/virtualfile/VirtualFile.java
// public interface VirtualFile {
// 
//     public VirtualFile getFile(String relativePath);
// 
//     public InputStream createInputStream();
// 
//     public OutputStream createOutputStream();
// 
//     public boolean exists();
// }
// 
// Path: src/main/java/be/bagofwords/virtualfile/VirtualFileService.java
// public abstract class VirtualFileService {
// 
//     public abstract VirtualFile getRootDirectory();
// 
//     public void storeSingleObject(String location, Object object) {
//         VirtualFile file = getFile(location);
//         try (OutputStream os = file.createOutputStream()) {
//             SerializationUtils.writeObject(object, os);
//         } catch (IOException exp) {
//             throw new RuntimeException("Unexpected exception while trying to write object to " + location);
//         }
//     }
// 
//     public <T> T readSingleObject(String location, Class<T> _class) {
//         VirtualFile file = getFile(location);
//         try (InputStream is = file.createInputStream()) {
//             return SerializationUtils.readObject(_class, is);
//         } catch (IOException exp) {
//             throw new RuntimeException("Unexpected exception while trying to write object to " + location);
//         }
//     }
// 
//     private VirtualFile getFile(String location) {
//         return getRootDirectory().getFile(location);
//     }
// }
// 
// Path: src/main/java/be/bagofwords/db/remote/Protocol.java
// public static final long LONG_ERROR = Long.MAX_VALUE - 2;
// 
// Path: src/main/java/be/bagofwords/db/remote/Protocol.java
// public static final long LONG_OK = Long.MAX_VALUE - 1;


import be.bagofwords.logging.Log;
import be.bagofwords.minidepi.ApplicationContext;
import be.bagofwords.util.SocketConnection;
import be.bagofwords.virtualfile.VirtualFile;
import be.bagofwords.virtualfile.VirtualFileService;
import be.bagofwords.web.SocketRequestHandler;
import be.bagofwords.web.SocketRequestHandlerFactory;
import be.bagofwords.web.SocketServer;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static be.bagofwords.db.remote.Protocol.LONG_ERROR;
import static be.bagofwords.db.remote.Protocol.LONG_OK;
        }

        @Override
        public long getTotalNumberOfRequests() {
            return totalNumberOfRequests;
        }

        @Override
        public void handleRequests() throws Exception {
            //We only handle a single request:
            byte actionAsByte = connection.readByte();
            try {
                Action action = Action.values()[actionAsByte];
                if (action == Action.INPUT_STREAM) {
                    String relPath = connection.readString();
                    VirtualFile file = virtualFileService.getRootDirectory().getFile(relPath);
                    if (file.exists()) {
                        InputStream is = file.createInputStream();
                        connection.writeLong(LONG_OK);
                        IOUtils.copy(is, connection.getOs());
                        connection.flush();
                    } else {
                        connection.writeLong(LONG_ERROR);
                        connection.writeString("Could not find file " + relPath);
                        connection.flush();
                    }
                } else if (action == Action.OUTPUT_STREAM) {
                    String relPath = connection.readString();
                    VirtualFile file = virtualFileService.getRootDirectory().getFile(relPath);
                    OutputStream os = file.createOutputStream();
// Path: src/main/java/be/bagofwords/virtualfile/VirtualFile.java
// public interface VirtualFile {
// 
//     public VirtualFile getFile(String relativePath);
// 
//     public InputStream createInputStream();
// 
//     public OutputStream createOutputStream();
// 
//     public boolean exists();
// }
// 
// Path: src/main/java/be/bagofwords/virtualfile/VirtualFileService.java
// public abstract class VirtualFileService {
// 
//     public abstract VirtualFile getRootDirectory();
// 
//     public void storeSingleObject(String location, Object object) {
//         VirtualFile file = getFile(location);
//         try (OutputStream os = file.createOutputStream()) {
//             SerializationUtils.writeObject(object, os);
//         } catch (IOException exp) {
//             throw new RuntimeException("Unexpected exception while trying to write object to " + location);
//         }
//     }
// 
//     public <T> T readSingleObject(String location, Class<T> _class) {
//         VirtualFile file = getFile(location);
//         try (InputStream is = file.createInputStream()) {
//             return SerializationUtils.readObject(_class, is);
//         } catch (IOException exp) {
//             throw new RuntimeException("Unexpected exception while trying to write object to " + location);
//         }
//     }
// 
//     private VirtualFile getFile(String location) {
//         return getRootDirectory().getFile(location);
//     }
// }
// 
// Path: src/main/java/be/bagofwords/db/remote/Protocol.java
// public static final long LONG_ERROR = Long.MAX_VALUE - 2;
// 
// Path: src/main/java/be/bagofwords/db/remote/Protocol.java
// public static final long LONG_OK = Long.MAX_VALUE - 1;


// Path: src/main/java/be/bagofwords/virtualfile/remote/RemoteFileServer.java
import be.bagofwords.logging.Log;
import be.bagofwords.minidepi.ApplicationContext;
import be.bagofwords.util.SocketConnection;
import be.bagofwords.virtualfile.VirtualFile;
import be.bagofwords.virtualfile.VirtualFileService;
import be.bagofwords.web.SocketRequestHandler;
import be.bagofwords.web.SocketRequestHandlerFactory;
import be.bagofwords.web.SocketServer;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static be.bagofwords.db.remote.Protocol.LONG_ERROR;
import static be.bagofwords.db.remote.Protocol.LONG_OK;

        }

        @Override
        public long getTotalNumberOfRequests() {
            return totalNumberOfRequests;
        }

        @Override
        public void handleRequests() throws Exception {
            //We only handle a single request:
            byte actionAsByte = connection.readByte();
            try {
                Action action = Action.values()[actionAsByte];
                if (action == Action.INPUT_STREAM) {
                    String relPath = connection.readString();
                    VirtualFile file = virtualFileService.getRootDirectory().getFile(relPath);
                    if (file.exists()) {
                        InputStream is = file.createInputStream();
                        connection.writeLong(LONG_OK);
                        IOUtils.copy(is, connection.getOs());
                        connection.flush();
                    } else {
                        connection.writeLong(LONG_ERROR);
                        connection.writeString("Could not find file " + relPath);
                        connection.flush();
                    }
                } else if (action == Action.OUTPUT_STREAM) {
                    String relPath = connection.readString();
                    VirtualFile file = virtualFileService.getRootDirectory().getFile(relPath);
                    OutputStream os = file.createOutputStream();
                    connection.writeLong(LONG_OK);