blob: 1cd184ad0fd5efc4715f2ee70cb59a37344746bd [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016 Oak Ridge National Laboratory and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.remote.internal.proxy.server.commands;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.remote.proxy.core.MultiplexedChannel;
import org.eclipse.remote.proxy.core.exceptions.ProxyException;
/**
* TODO: Fix hang if command fails...
*
*/
public class ServerGetOutputStreamCommand extends AbstractServerCommand {
private final InputStream in;
private final URI uri;
private final int options;
private class Forwarder implements Runnable {
private final InputStream in;
private final OutputStream out;
public Forwarder(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}
@Override
public void run() {
byte[] buf = new byte[8192];
int n;
try {
while ((n = in.read(buf)) >= 0) {
if (n > 0) {
out.write(buf, 0, n); // should block if no-one is reading
out.flush();
}
}
} catch (IOException e) {
// Finish
}
try {
in.close();
} catch (IOException e) {
// Ignore
}
}
}
public ServerGetOutputStreamCommand(MultiplexedChannel chan, int options, String path) {
this.in = chan.getInputStream();
this.options = options;
this.uri = URI.create("file:" + path);
}
public void exec() throws ProxyException {
try {
OutputStream out = EFS.getStore(uri).openOutputStream(options, new NullProgressMonitor());
startForwarder(in, out);
} catch (CoreException e) {
new ProxyException(e.getMessage());
}
}
private void startForwarder(InputStream in, OutputStream out) {
Forwarder forwarder = new Forwarder(in, out);
new Thread(forwarder).start();
}
}