Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xnio;
import java.io.Closeable;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.Selector;
import java.nio.channels.Channel;
import java.nio.channels.WritableByteChannel;
import java.util.Random;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.zip.ZipFile;
import org.xnio.channels.SuspendableReadChannel;
import java.util.logging.Handler;
import static org.xnio._private.Messages.closeMsg;
import static org.xnio._private.Messages.msg;
/**
* General I/O utility methods.
*
* @apiviz.exclude
*/
public final class IoUtils {
private static final Executor NULL_EXECUTOR = new Executor() {
private final String string = String.format("null executor <%s>", Integer.toHexString(hashCode()));
public void execute(final Runnable command) {
// no operation
}
public String toString() {
return string;
}
};
private static final Executor DIRECT_EXECUTOR = new Executor() {
private final String string = String.format("direct executor <%s>", Integer.toHexString(hashCode()));
public void execute(final Runnable command) {
command.run();
}
public String toString() {
return string;
}
};
private static final Closeable NULL_CLOSEABLE = new Closeable() {
private final String string = String.format("null closeable <%s>", Integer.toHexString(hashCode()));
public void close() throws IOException {
// no operation
}
public String toString() {
return string;
}
};
private static final Cancellable NULL_CANCELLABLE = new Cancellable() {
public Cancellable cancel() {
return this;
}
};
@SuppressWarnings("rawtypes")
private static final IoUtils.ResultNotifier RESULT_NOTIFIER = new IoUtils.ResultNotifier();
private IoUtils() {}
/**
* Get the direct executor. This is an executor that executes the provided task in the same thread.
*
* @return a direct executor
*/
public static Executor directExecutor() {
return DIRECT_EXECUTOR;
}
/**
* Get the null executor. This is an executor that never actually executes the provided task.
*
* @return a null executor
*/
public static Executor nullExecutor() {
return NULL_EXECUTOR;
}
/**
* Get the null closeable. This is a simple {@code Closeable} instance that does nothing when its {@code close()}
* method is invoked.
*
* @return the null closeable
*/
public static Closeable nullCloseable() {
return NULL_CLOSEABLE;
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final AutoCloseable resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (ClosedChannelException ignored) {
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final Closeable resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (ClosedChannelException ignored) {
msg.tracef("safeClose, ignoring ClosedChannelException exception");
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a series of resources, logging errors if they occur.
*
* @param resources the resources to close
*/
public static void safeClose(final Closeable... resources) {
for (Closeable resource : resources) {
safeClose(resource);
}
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final Socket resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (ClosedChannelException ignored) {
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final DatagramSocket resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final Selector resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (ClosedChannelException ignored) {
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final ServerSocket resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (ClosedChannelException ignored) {
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final ZipFile resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a resource, logging an error if an error occurs.
*
* @param resource the resource to close
*/
public static void safeClose(final Handler resource) {
try {
if (resource != null) {
closeMsg.closingResource(resource);
resource.close();
}
} catch (Throwable t) {
closeMsg.resourceCloseFailed(t, resource);
}
}
/**
* Close a future resource, logging an error if an error occurs. Attempts to cancel the operation if it is
* still in progress.
*
* @param futureResource the resource to close
*/
public static void safeClose(final IoFuture extends Closeable> futureResource) {
if (futureResource != null) {
futureResource.cancel().addNotifier(closingNotifier(), null);
}
}
private static final IoFuture.Notifier