use of java.nio.channels.ClosedChannelException in project eiger by wlloyd.
the class RandomAccessReader method read.
@Override
public // or readFully (from RandomAccessFile) will throw EOFException but this should not
int read(byte[] buff, int offset, int length) throws IOException {
if (buffer == null)
throw new ClosedChannelException();
if (length == 0)
return 0;
if (isEOF())
return -1;
if (current >= bufferOffset + buffer.length || validBufferBytes == -1)
reBuffer();
assert current >= bufferOffset && current < bufferOffset + validBufferBytes : String.format("File (%s), current offset %d, buffer offset %d, buffer limit %d", getPath(), current, bufferOffset, validBufferBytes);
int toCopy = Math.min(length, validBufferBytes - bufferCursor());
System.arraycopy(buffer, bufferCursor(), buff, offset, toCopy);
current += toCopy;
return toCopy;
}
use of java.nio.channels.ClosedChannelException in project eiger by wlloyd.
the class SequentialWriter method write.
public void write(byte[] data, int offset, int length) throws IOException {
if (buffer == null)
throw new ClosedChannelException();
while (length > 0) {
int n = writeAtMost(data, offset, length);
offset += n;
length -= n;
isDirty = true;
syncNeeded = true;
}
}
use of java.nio.channels.ClosedChannelException in project cloudstack by apache.
the class AgentManagerImpl method connectAgent.
protected void connectAgent(final Link link, final Command[] cmds, final Request request) {
// send startupanswer to agent in the very beginning, so agent can move on without waiting for the answer for an undetermined time, if we put this logic into another
// thread pool.
final StartupAnswer[] answers = new StartupAnswer[cmds.length];
Command cmd;
for (int i = 0; i < cmds.length; i++) {
cmd = cmds[i];
if (cmd instanceof StartupRoutingCommand || cmd instanceof StartupProxyCommand || cmd instanceof StartupSecondaryStorageCommand || cmd instanceof StartupStorageCommand) {
answers[i] = new StartupAnswer((StartupCommand) cmds[i], 0, getPingInterval());
break;
}
}
Response response = null;
response = new Response(request, answers[0], _nodeId, -1);
try {
link.send(response.toBytes());
} catch (final ClosedChannelException e) {
s_logger.debug("Failed to send startupanswer: " + e.toString());
}
_connectExecutor.execute(new HandleAgentConnectTask(link, cmds, request));
}
use of java.nio.channels.ClosedChannelException in project cloudstack by apache.
the class Agent method stop.
public void stop(final String reason, final String detail) {
s_logger.info("Stopping the agent: Reason = " + reason + (detail != null ? ": Detail = " + detail : ""));
if (_connection != null) {
final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
try {
if (_link != null) {
final Request req = new Request(_id != null ? _id : -1, -1, cmd, false);
_link.send(req.toBytes());
}
} catch (final ClosedChannelException e) {
s_logger.warn("Unable to send: " + cmd.toString());
} catch (final Exception e) {
s_logger.warn("Unable to send: " + cmd.toString() + " due to exception: ", e);
}
s_logger.debug("Sending shutdown to management server");
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
s_logger.debug("Who the heck interrupted me here?");
}
_connection.stop();
_connection = null;
}
if (_resource != null) {
_resource.stop();
_resource = null;
}
_ugentTaskPool.shutdownNow();
}
use of java.nio.channels.ClosedChannelException in project robovm by robovm.
the class ConcurrentCloseTest method test_connect_nonBlocking.
public void test_connect_nonBlocking() throws Exception {
StuckServer ss = new StuckServer(false);
SocketChannel s = SocketChannel.open();
new Killer(s.socket()).start();
try {
System.err.println("connect (non-blocking)...");
s.configureBlocking(false);
s.connect(ss.getLocalSocketAddress());
while (!s.finishConnect()) {
// Spin like a mad thing!
}
fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} catch (AsynchronousCloseException alsoOkay) {
// See below.
} catch (ClosedChannelException alsoOkay) {
// For now, I'm assuming that we're happy as long as we get any reasonable exception.
// It may be that we're supposed to guarantee only one or the other.
} finally {
ss.close();
}
}
Aggregations