public void read(double[] data, int howmany) throws IOException { if (!initialized) { init(); } byteBuffer.position(positionInBytes); doubleBuffer.position(positionInBytes / MemHolder.DOUBLESIZE); // fill up int dstPosition = 0; if (doubleBuffer.remaining() >= howmany) { doubleBuffer.get(data, 0, howmany); } else { // fill from remainder int remaining = doubleBuffer.remaining(); doubleBuffer.get(data, 0, remaining); dstPosition += remaining; while (dstPosition < howmany - doubleBuffer.capacity()) { byteBuffer.position(0); channel.read(byteBuffer); doubleBuffer.position(0); doubleBuffer.get(data, dstPosition, doubleBuffer.capacity()); dstPosition += doubleBuffer.capacity(); } byteBuffer.position(0); channel.read(byteBuffer); doubleBuffer.position(0); doubleBuffer.get(data, dstPosition, howmany - dstPosition); } positionInBytes = doubleBuffer.position() * MemHolder.DOUBLESIZE; }
@Override protected void processKey(SelectionKey key) throws IOException { if (key.isAcceptable()) { if (key.channel() == commandServerChannel) { acceptCommand(key); } else { if (commandChannel == null) { throw new IOException("Unexpected"); } accept(key); } } else { if (commandChannel == null) { throw new IOException("Unexpected"); } if (key.isReadable() && pendingChannels.containsKey(key.channel())) { ByteChannel channel = (ByteChannel) key.channel(); try { ByteBuffer readBuffer = pendingChannels.get(channel); int read = channel.read(readBuffer); if (read == -1) { throw new IOException("Pending channel closed"); } if (readBuffer.position() >= 5) { readBuffer.flip(); try { byte cmd = readBuffer.get(); if (cmd == CMD_OPEN_CHANNEL_ACK) { int tunnelId = readBuffer.getInt(); try { registerLeftChannel(tunnelId, channel); pendingChannels.remove(channel); } catch (IOException e) { logger.log(Level.INFO, "Spooling error: " + e.getMessage(), e); closeTunnel(tunnelId); } } else { throw new IOException("Unknown command"); } } finally { readBuffer.compact(); } } } catch (IOException e) { logger.log(Level.SEVERE, "PROTOCOL ERROR: " + e.getMessage(), e); close(channel); } } else { super.processKey(key); } } }
public void close(ThreadContext tc) { try { chan.close(); } catch (IOException e) { throw ExceptionHandling.dieInternal(tc, e); } }
public synchronized String slurp(ThreadContext tc) { try { // Read in file. ArrayList<ByteBuffer> buffers = new ArrayList<ByteBuffer>(); ByteBuffer curBuffer = ByteBuffer.allocate(32768); int total = 0; int read; if (readBuffer != null) { total = readBuffer.limit() - readBuffer.position(); buffers.add(ByteBuffer.wrap(readBuffer.array(), readBuffer.position(), total)); readBuffer = null; } while ((read = chan.read(curBuffer)) != -1) { curBuffer.flip(); buffers.add(curBuffer); curBuffer = ByteBuffer.allocate(32768); total += read; } eof = true; return decodeBuffers(buffers, total); } catch (IOException e) { throw ExceptionHandling.dieInternal(tc, e); } }
/** * @param remote Indicates who "generated" <code>code</code>.<br> * <code>true</code> means that this endpoint received the <code>code</code> from the other * endpoint.<br> * false means this endpoint decided to send the given code,<br> * <code>remote</code> may also be true if this endpoint started the closing handshake since * the other endpoint may not simply echo the <code>code</code> but close the connection the * same time this endpoint does do but with an other <code>code</code>. <br> */ protected synchronized void closeConnection(int code, String message, boolean remote) { if (readystate == READYSTATE.CLOSED) { return; } if (key != null) { // key.attach( null ); //see issue #114 key.cancel(); } if (channel != null) { try { channel.close(); } catch (IOException e) { wsl.onWebsocketError(this, e); } } try { this.wsl.onWebsocketClose(this, code, message, remote); } catch (RuntimeException e) { wsl.onWebsocketError(this, e); } if (draft != null) draft.reset(); handshakerequest = null; readystate = READYSTATE.CLOSED; this.outQueue.clear(); }
public synchronized int fillFrom(ByteChannel channel) throws IOException { if (buffer == null) { buffer = bufferFactory.newBuffer(); } return channel.read(buffer); }
public void stop() { try { channel.close(); logger.debug("~~ [CLOSE]"); } catch (IOException e) { throw new ClientException("Unable to close socket connection properly." + e.getMessage(), e); } }
private void init() throws IOException { channel.read(byteBuffer); intBuffer.position(0); doubleBuffer.position(0); byteBuffer.position(0); positionInBytes = 0; initialized = true; }
private void close(ByteChannel channel) { pendingChannels.remove(channel); try { channel.close(); } catch (IOException e) { // Best effort } }
protected void write(ThreadContext tc, ByteBuffer buffer) { try { int toWrite = buffer.limit(); int written = 0; while (written < toWrite) { written += chan.write(buffer); } } catch (IOException e) { throw ExceptionHandling.dieInternal(tc, e); } }
public byte[] read(ThreadContext tc, int bytes) { try { ByteBuffer buffer = ByteBuffer.allocate(bytes); chan.read(buffer); buffer.flip(); byte[] res = new byte[buffer.limit()]; buffer.get(res); return res; } catch (IOException e) { throw ExceptionHandling.dieInternal(tc, e); } }
public synchronized String readline(ThreadContext tc) { try { boolean foundLine = false; ArrayList<ByteBuffer> lineChunks = new ArrayList<ByteBuffer>(); int total = 0; while (!foundLine) { /* Ensure we have a buffer available. */ if (readBuffer == null) { readBuffer = ByteBuffer.allocate(32768); if (chan.read(readBuffer) == -1) { /* End of file, so what we have is fine. */ eof = true; foundLine = true; readBuffer.flip(); break; } readBuffer.flip(); } /* Look for a line end. */ int start = readBuffer.position(); int end = start; while (!foundLine && end < readBuffer.limit()) { int index = 0; while (index < linesep.length && end + index < readBuffer.limit() && readBuffer.get(end + index) == linesep[index]) index++; if (index == linesep.length) { end += index; foundLine = true; } else { end++; } } /* Copy what we found into the results. */ byte[] lineBytes = new byte[end - start]; readBuffer.get(lineBytes); lineChunks.add(ByteBuffer.wrap(lineBytes)); total += lineBytes.length; /* If we didn't find a line, will cross chunk boundary. */ if (!foundLine) readBuffer = null; } if (lineChunks.size() == 1) return dec.decode(lineChunks.get(0)).toString(); else return decodeBuffers(lineChunks, total); } catch (IOException e) { throw ExceptionHandling.dieInternal(tc, e); } }
public static void main(String[] args) throws IOException { Path pIn = FileSystems.getDefault().getPath(args[0]); Path pOut = FileSystems.getDefault().getPath(args[1]); ByteChannel in = Files.newByteChannel(pIn, StandardOpenOption.READ); ByteChannel out = Files.newByteChannel(pOut, StandardOpenOption.CREATE, StandardOpenOption.WRITE); ByteBuffer bb = ByteBuffer.allocate(BUFFER_SIZE); int nb = 0; while ((nb = in.read(bb)) != -1) { bb.flip(); while (bb.hasRemaining()) { byte b = bb.get(); ByteBuffer w; if (b == CONST) { byte[] arr = {b, b}; w = ByteBuffer.wrap(arr); } else { byte[] arr = {b}; w = ByteBuffer.wrap(arr); } out.write(w); } bb.clear(); } in.close(); out.close(); }
private SocketProtocol negotiateProtocol() throws IOException { // TODO make this not so hard-coded logger.debug("~~ [HANDSHAKE] [1, 0, 0, 0]."); // Propose protocol versions ByteBuffer buf = ByteBuffer.wrap( new byte[] { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); channel.write(buf); // Read back the servers choice buf.clear(); buf.limit(4); channel.read(buf); // Choose protocol, or fail buf.flip(); final int proposal = buf.getInt(); switch (proposal) { case 1: logger.debug("~~ [HANDSHAKE] 1"); return new SocketProtocolV1(channel); case 0: throw new ClientException( "The server does not support any of the protocol versions supported by " + "this driver. Ensure that you are using driver and server versions that " + "are compatible with one another."); default: throw new ClientException( "Protocol error, server suggested unexpected protocol version: " + proposal); } }
public synchronized String getc(ThreadContext tc) { try { int maxBytes = (int) enc.maxBytesPerChar(); ByteBuffer toDecode = ByteBuffer.allocate(maxBytes); CharBuffer decoded = CharBuffer.allocate(1); for (int i = 0; i < maxBytes; i++) { /* Ensure we have a read buffer available. */ if (readBuffer == null) { readBuffer = ByteBuffer.allocate(32768); if (chan.read(readBuffer) == -1) { /* End of file. */ eof = true; if (i == 0) { /* Fine, just no char. */ return ""; } else { /* Malformed; following will likely throw. */ toDecode.position(0); dec.decode(toDecode, decoded, true).throwException(); return decoded.toString(); } } readBuffer.flip(); } /* Get a character from the read buffer. */ toDecode.position(i); toDecode.put(readBuffer.get()); if (readBuffer.remaining() == 0) readBuffer = null; /* Try to decode; if we fail, try another byte. */ toDecode.position(0); CoderResult cr = dec.decode(toDecode, decoded, false); if (!cr.isError()) { decoded.rewind(); return decoded.toString(); } } throw new MalformedInputException(maxBytes); } catch (IOException e) { throw ExceptionHandling.dieInternal(tc, e); } }
public synchronized int drainTo(ByteChannel channel) throws IOException { int bytesWritten = 0; while (true) { if (active == null) { if (queue.size() == 0) break; active = queue.removeFirst(); active.flip(); } int rc = channel.write(active); bytesWritten += rc; if (!active.hasRemaining()) { bufferFactory.returnBuffer(active); active = null; } if (rc == 0) break; } return bytesWritten; }
@Override public void close() throws IOException { socket.close(); }
public int writeToChannel(ByteChannel channel) throws IOException { buf.flip(); int written = channel.write(buf); buf.clear(); return written; }
@Override public void serialize(final ByteChannel channel, final Character data) throws IOException { final ByteBuffer buffer = Charset.forName("UTF-16BE").encode(String.valueOf(data.charValue())); channel.write(buffer); }
@Override public int read(ByteBuffer dst) throws IOException { return socket.read(dst); }
@Override public boolean isOpen() { return socket.isOpen(); }
@Override public int write(ByteBuffer src) throws IOException { return socket.write(src); }