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();
 }
Example #2
0
 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);
   }
 }
  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);
    }
  }
Example #4
0
  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;
  }
Example #5
0
 @Override
 public int write(ByteBuffer src) throws IOException {
   return socket.write(src);
 }
 @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);
 }
 public int writeToChannel(ByteChannel channel) throws IOException {
   buf.flip();
   int written = channel.write(buf);
   buf.clear();
   return written;
 }