Minecraft-SSHD/Minecraft-SSHD-Bukkit/src/main/java/com/ryanmichela/sshd/FlushyOutputStream.java

60 lines
1012 B
Java
Raw Normal View History

2013-11-14 07:17:51 +00:00
package com.ryanmichela.sshd;
import org.apache.sshd.common.SshException;
2019-10-06 09:50:06 +00:00
import org.apache.sshd.common.channel.exception.SshChannelClosedException;
2013-11-14 07:17:51 +00:00
import java.io.IOException;
import java.io.OutputStream;
2019-10-06 09:50:06 +00:00
import java.math.BigInteger;
2013-11-14 07:17:51 +00:00
/**
* Copyright 2013 Ryan Michela
*/
2019-10-06 09:50:06 +00:00
public class FlushyOutputStream extends OutputStream
{
private OutputStream base;
private boolean isClosed = false;
2019-10-06 09:50:06 +00:00
public FlushyOutputStream(OutputStream base)
{
this.base = base;
}
@Override
2019-10-06 09:50:06 +00:00
public void write(int b) throws IOException
{
this.write(BigInteger.valueOf(b).toByteArray());
}
@Override
2019-10-06 09:50:06 +00:00
public void write(byte[] b) throws IOException
{
this.write(b, 0, b.length);
}
@Override
2019-10-06 09:50:06 +00:00
public void write(byte[] b, int off, int len) throws IOException
{
if (isClosed)
return;
try
{
base.write(b, off, len);
base.flush();
2019-10-06 09:50:06 +00:00
}
catch (SshChannelClosedException e)
{
// ignored.
}
}
@Override
2019-10-06 09:50:06 +00:00
public void close() throws IOException
{
isClosed = true;
2019-10-06 09:50:06 +00:00
base.close();
}
2013-11-14 07:17:51 +00:00
}