@Override public void handleException(Channel channel, IOException exception) { IoUtils.safeClose(channel); if (exchange.isResponseStarted()) { IoUtils.safeClose(clientConnection); UndertowLogger.REQUEST_IO_LOGGER.debug("Exception reading from target server", exception); if (!exchange.isResponseStarted()) { exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR); exchange.endExchange(); } else { IoUtils.safeClose(exchange.getConnection()); } } else { exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR); exchange.endExchange(); } }
public static synchronized Channel getChannel() throws IOException { if (devnull < 0) { devnull = open0("/dev/null", O_RDWR); } // If we don't have the channel try to create it if (!haveChannel) { channel = createChannel(); haveChannel = true; } // if there is a channel then do the security check before // returning it. if (channel != null) { checkAccess(channel); } return channel; }
public synchronized Channel inheritedChannel() throws IOException { System.err.println("SP.inheritedChannel"); if (channel == null) { channel = SocketChannel.open(); Socket socket = channel.socket(); System.err.println("socket = " + socket); /* * Notify test that inherited channel was created. */ try { System.err.println("notify test..."); Registry registry = LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT); Callback obj = (Callback) registry.lookup("Callback"); obj.notifyTest(); } catch (NotBoundException nbe) { throw (IOException) new IOException("callback object not bound"). initCause(nbe); } } return channel; }
public static void main(String args[]) { // test the assertion that SelectorProvider.inheritedChannel() // and System.inheritedChannel return null when standard input // is not connected to a socket Channel c1, c2; try { c1 = SelectorProvider.provider().inheritedChannel(); c2 = System.inheritedChannel(); } catch (IOException ioe) { throw new RuntimeException("Unexpected IOException: " + ioe); } if (c1 != null || c2 != null) { throw new RuntimeException("Channel returned - unexpected"); } }
public void releaseFileLock() { if (fileLock != null) { if (log.isTraceEnable()) { log.info(this, "Releasing the file lock of " + this.filePath.getFileName()); } Channel fc = fileLock.acquiredBy(); try { fileLock.release(); fileLock = null; if (fc != null) { fc.close(); } } catch (IOException e) { } } }
public synchronized Channel inheritedChannel() throws IOException { System.err.println("SP.inheritedChannel"); if (channel == null) { channel = SocketChannel.open(); Socket socket = channel.socket(); System.err.println("socket = " + socket); /* * Notify test that inherited channel was created. */ try { System.err.println("notify test..."); int registryPort = Integer.getInteger( "test.java.rmi.rmidViaInheritedChannel.registry.port", 0); Registry registry = LocateRegistry.getRegistry(registryPort); Callback obj = (Callback) registry.lookup("Callback"); obj.notifyTest(); } catch (NotBoundException nbe) { throw (IOException) new IOException("callback object not bound"). initCause(nbe); } } return channel; }
@Override public void completed(final ClientConnection connection) { final ServerConnection serverConnection = exchange.getConnection(); //we attach to the connection so it can be re-used serverConnection.putAttachment(clientAttachmentKey, connection); serverConnection.addCloseListener(serverConnection1 -> IoUtils.safeClose(connection)); connection.getCloseSetter().set((ChannelListener<Channel>) channel -> serverConnection.removeAttachment(clientAttachmentKey)); exchange.setRelativePath("/"); Realm realm = realmCache.matches(exchange.getRequestPath()); Application application = realmCache.getApplication(realm); String path = exchange.getRequestURI(); if (path.startsWith(application.getVirtualPath())) { String passTo = calculatePathTo(path, application); exchange.setRequestPath(passTo); exchange.setRequestURI(passTo); } callback.completed(exchange, new ProxyConnection(connection, "/")); }
/** * Internal fsync implementation. */ private static void fsync(PyObject fd, boolean metadata) { RawIOBase rawIO = FileDescriptors.get(fd); rawIO.checkClosed(); Channel channel = rawIO.getChannel(); if (!(channel instanceof FileChannel)) { throw Py.OSError(Errno.EINVAL); } try { ((FileChannel)channel).force(metadata); } catch (ClosedChannelException cce) { // In the rare case it's closed but the rawIO wasn't throw Py.ValueError("I/O operation on closed file"); } catch (IOException ioe) { throw Py.OSError(ioe); } }
private void doClose() { connLock.lock(); try { Channel channel = this.channel; if (channel != null) { if (channel.isOpen()) { IOUtils.close(channel); listener.onDisConnected(client); logger.w("channel closed !!!"); } this.channel = null; } } finally { state.set(disconnected); connLock.unlock(); } }
public void startListening() { while (isAlive) { Channel channel; channel = network.choosingConnection(); // first stop here! if (channel instanceof ServerSocketChannel) { //System.out.println("TCP received!"); // TODO: Read TCP received message, convert to enum and reply in another class. TcpReceiver tcpReceiver = new TcpReceiver(channel); new Thread(tcpReceiver).start(); } else if (channel instanceof DatagramChannel) { System.out.println("UDP received!"); // TODO: Read UDP received message, convert to enum and reply in another class. } else { System.out.println("meh"); } } }
@Override public Channel choosingConnection() { try { selector.select(); // stop here! //System.out.println("Selector chose something!"); Set<SelectionKey> keys = selector.selectedKeys(); for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) { SelectionKey key = (SelectionKey) i.next(); i.remove(); Channel c = (Channel) key.channel(); if (key.isAcceptable() && c == tcpChannel) return tcpChannel; else if (key.isReadable() && c == udpChannel) return udpChannel; } } catch (IOException e) { e.printStackTrace(); } return null; }
/** * Closes a channel. Channel can be null and any IOException's will be swallowed. * * @param channel The stream to close. */ public static void close( Channel channel ) { if ( channel == null ) { return; } try { channel.close(); } catch ( IOException ex ) { // ignore } }
@Override protected void doReleaseExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception { // must call super super.doReleaseExclusiveReadLock(operations, file, exchange); FileLock lock = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), FileLock.class); RandomAccessFile rac = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), RandomAccessFile.class); String target = file.getFileName(); if (lock != null) { Channel channel = lock.acquiredBy(); try { lock.release(); } finally { // close channel as well IOHelper.close(channel, "while releasing exclusive read lock for file: " + target, LOG); IOHelper.close(rac, "while releasing exclusive read lock for file: " + target, LOG); } } }
/** * This should be followed by closing a selector. * * @param channel the channel to close. */ public static void closeHard(final Channel channel) { if (channel != null) { try { // Close socket if (channel instanceof SocketChannel) { closeHard(((SocketChannel) channel).socket()); } // Close channel channel.close(); } catch (final Exception e) { ignoreException(e, "closing hard"); } } }
@Test public void test(){ ByteBuffer byteBuffer = ByteBuffer.allocate(10); byteBuffer.put((byte) 'a'); byteBuffer.put((byte) 'b'); byteBuffer.put((byte) 'c'); Channel channel = Channels.newChannel(new InputStream() { @Override public int read() throws IOException { return 0; } }); CharBuffer charBuffer = CharBuffer.allocate(10); charBuffer.put('a'); charBuffer.put('b'); charBuffer.put('c'); System.out.println(charBuffer.get()); }
public void createFileHandler() { TimeCacheMap.ExpiredCallback<Object, Object> expiredCallback = new TimeCacheMap.ExpiredCallback<Object, Object>() { @Override public void expire(Object key, Object val) { try { LOG.info("Close file " + String.valueOf(key)); if (val != null) { if (val instanceof Channel) { Channel channel = (Channel) val; channel.close(); } else if (val instanceof BufferFileInputStream) { BufferFileInputStream is = (BufferFileInputStream) val; is.close(); } } } catch (IOException e) { LOG.error(e.getMessage(), e); } } }; int file_copy_expiration_secs = JStormUtils.parseInt(conf.get(Config.NIMBUS_FILE_COPY_EXPIRATION_SECS), 30); uploaders = new TimeCacheMap<Object, Object>(file_copy_expiration_secs, expiredCallback); downloaders = new TimeCacheMap<Object, Object>(file_copy_expiration_secs, expiredCallback); }
@Test public void testQuickSort() throws Exception{ DiscBasedSort <String> discBasedSort = new DiscBasedSort <> (codec, 1024,4096,1,Platform.getTempDir().resolve("sort")); //stringBuffer.flip(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(stringBuffer.array()); Channel c = Channels.newChannel(bis); Channel co = Channels.newChannel(bos); int num = discBasedSort.sort((ReadableByteChannel)c, (WritableByteChannel) co); //edb.sort(Channels.newChannel(c, bos, edb.getBlockSize()*1); //verify that things are the same //Collator col = Collator.getInstance(new Locale("sv")); Collections.sort(stringList); String tStr; bos.flush(); ByteBuffer sorted = ByteBuffer.wrap(bos.toByteArray()); for(String str : stringList){ tStr = codec.from(sorted); assertEquals(str, tStr); } }
public FileLock tryLock(Channel channel, long start, long size, boolean shared) throws IOException{ if(!channel.isOpen()) { throw new ClosedChannelException(); } Iterator<EphemeralFsFileLock> iter = locks.iterator(); while(iter.hasNext()) { EphemeralFsFileLock oldLock = iter.next(); if(!oldLock.isValid()) { iter.remove(); } else if(oldLock.overlaps(start, size)) { throw new OverlappingFileLockException(); } } EphemeralFsFileLock newLock = channel instanceof FileChannel ? new EphemeralFsFileLock((FileChannel) channel, start, size, shared) : new EphemeralFsFileLock((AsynchronousFileChannel) channel, start, size, shared); locks.add(newLock); return newLock; }
public synchronized Channel inheritedChannel() throws IOException { System.err.println("SP.inheritedChannel"); if (channel == null) { channel = SocketChannel.open(); Socket socket = channel.socket(); System.err.println("socket = " + socket); /* * Notify test that inherited channel was created. */ try { System.err.println("notify test..."); Registry registry = LocateRegistry.getRegistry(TestLibrary.REGISTRY_PORT); Callback obj = (Callback) registry.lookup("Callback"); obj.notifyTest(); } catch (NotBoundException nbe) { throw (IOException) new IOException("callback object not bound"). initCause(nbe); } } return channel; }