public void write(Tag tag, RandomAccessFile raf, RandomAccessFile tempRaf)
      throws CannotWriteException, IOException {
    FileChannel fc = raf.getChannel();

    int oldTagSize = 0;

    if (tagExists(fc)) {
      // read the length
      if (!canOverwrite(raf))
        throw new CannotWriteException("Overwritting of this kind of ID3v2 tag not supported yet");
      fc.position(6);

      ByteBuffer buf = ByteBuffer.allocate(4);
      fc.read(buf);
      oldTagSize = (buf.get(0) & 0xFF) << 21;
      oldTagSize += (buf.get(1) & 0xFF) << 14;
      oldTagSize += (buf.get(2) & 0xFF) << 7;
      oldTagSize += buf.get(3) & 0xFF;
      oldTagSize += 10;

      // System.err.println("Old tag size: "+oldTagSize);
      int newTagSize = tc.getTagLength(tag);

      if (oldTagSize >= newTagSize) {
        // replace
        // System.err.println("Old ID32v Tag found, replacing the old
        // tag");
        fc.position(0);

        fc.write(tc.convert(tag, oldTagSize - newTagSize));

        // ID3v2 Tag Written

        return;
      }
    }

    // create new tag with padding
    // System.err.println("Creating a new ID3v2 Tag");
    fc.position(oldTagSize);

    if (fc.size() > 15 * 1024 * 1024) {
      FileChannel tempFC = tempRaf.getChannel();

      tempFC.position(0);
      tempFC.write(tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING));
      tempFC.transferFrom(fc, tempFC.position(), fc.size() - oldTagSize);

      fc.close();
    } else {
      ByteBuffer[] content = new ByteBuffer[2];

      content[1] = ByteBuffer.allocate((int) fc.size());
      fc.read(content[1]);
      content[1].rewind();
      content[0] = tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING);
      fc.position(0);
      fc.write(content);
    }
  }
  public static void main(String[] argv) throws Exception {
    Pipe[] pipes = new Pipe[PIPES_COUNT];
    Pipe pipe = Pipe.open();
    Pipe.SinkChannel sink = pipe.sink();
    Pipe.SourceChannel source = pipe.source();
    Selector sel = Selector.open();
    source.configureBlocking(false);
    source.register(sel, SelectionKey.OP_READ);

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i] = Pipe.open();
      Pipe.SourceChannel sc = pipes[i].source();
      sc.configureBlocking(false);
      sc.register(sel, SelectionKey.OP_READ);
      Pipe.SinkChannel sc2 = pipes[i].sink();
      sc2.configureBlocking(false);
      sc2.register(sel, SelectionKey.OP_WRITE);
    }

    for (int i = 0; i < LOOPS; i++) {
      sink.write(ByteBuffer.allocate(BUF_SIZE));
      int x = sel.selectNow();
      sel.selectedKeys().clear();
      source.read(ByteBuffer.allocate(BUF_SIZE));
    }

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i].sink().close();
      pipes[i].source().close();
    }
    pipe.sink().close();
    pipe.source().close();
    sel.close();
  }
Exemple #3
0
  public static void main(String args[]) {
    try {
      aServer asr = new aServer();

      // file channel.
      FileInputStream is = new FileInputStream("");
      is.read();
      FileChannel cha = is.getChannel();
      ByteBuffer bf = ByteBuffer.allocate(1024);
      bf.flip();

      cha.read(bf);

      // Path Paths
      Path pth = Paths.get("", "");

      // Files some static operation.
      Files.newByteChannel(pth);
      Files.copy(pth, pth);
      // file attribute, other different class for dos and posix system.
      BasicFileAttributes bas = Files.readAttributes(pth, BasicFileAttributes.class);
      bas.size();

    } catch (Exception e) {
      System.err.println(e);
    }

    System.out.println("hello ");
  }
Exemple #4
0
  public static void main(String args[]) throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(10);

    for (int i = 0; i < buffer.capacity(); ++i) {
      buffer.put((byte) i);
    }

    buffer.position(3);
    buffer.limit(7);

    ByteBuffer slice = buffer.slice();

    for (int i = 0; i < slice.capacity(); ++i) {
      byte b = slice.get(i);
      b *= 11;
      slice.put(i, b);
    }

    buffer.position(0);
    buffer.limit(buffer.capacity());

    while (buffer.hasRemaining()) {
      System.out.println(buffer.get());
    }
  }
Exemple #5
0
  // Check if the datagramsocket adaptor can send with a packet
  // that has not been initialized with an address; the legacy
  // datagram socket will send in this case
  private static void test2() throws Exception {
    DatagramChannel sndChannel = DatagramChannel.open();
    sndChannel.socket().bind(null);
    InetSocketAddress sender =
        new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort());

    DatagramChannel rcvChannel = DatagramChannel.open();
    rcvChannel.socket().bind(null);
    InetSocketAddress receiver =
        new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort());

    rcvChannel.connect(sender);
    sndChannel.connect(receiver);

    byte b[] = "hello".getBytes("UTF-8");
    DatagramPacket pkt = new DatagramPacket(b, b.length);
    sndChannel.socket().send(pkt);

    ByteBuffer bb = ByteBuffer.allocate(256);
    rcvChannel.receive(bb);
    bb.flip();
    CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed");

    // Check that the pkt got set with the target address;
    // This is legacy behavior
    if (!pkt.getSocketAddress().equals(receiver)) throw new RuntimeException("Test failed");

    rcvChannel.close();
    sndChannel.close();
  }
Exemple #6
0
  // Check if DatagramChannel.send while connected can include
  // address without throwing
  private static void test1() throws Exception {

    DatagramChannel sndChannel = DatagramChannel.open();
    sndChannel.socket().bind(null);
    InetSocketAddress sender =
        new InetSocketAddress(InetAddress.getLocalHost(), sndChannel.socket().getLocalPort());

    DatagramChannel rcvChannel = DatagramChannel.open();
    rcvChannel.socket().bind(null);
    InetSocketAddress receiver =
        new InetSocketAddress(InetAddress.getLocalHost(), rcvChannel.socket().getLocalPort());

    rcvChannel.connect(sender);
    sndChannel.connect(receiver);

    ByteBuffer bb = ByteBuffer.allocate(256);
    bb.put("hello".getBytes());
    bb.flip();
    int sent = sndChannel.send(bb, receiver);
    bb.clear();
    rcvChannel.receive(bb);
    bb.flip();
    CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
    if (!cb.toString().startsWith("h")) throw new RuntimeException("Test failed");

    rcvChannel.close();
    sndChannel.close();
  }
  /*
   * Creates a file at filename if file doesn't exist. Writes
   * value to that file using FileChannel.
   */
  public static void writeToFile(File filename, String value, String hideCommand)
      throws IOException, InterruptedException {
    if (!hideCommand.trim().equals("")) {
      // unhideFile(filename);
    }

    if (!filename.exists()) {
      filename.createNewFile();
    }

    byte[] bytes = value.getBytes();
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    for (int i = 0; i < bytes.length; i++) {
      buffer.put(bytes[i]);
    }
    buffer.rewind();

    try {

      fileWriter = new FileOutputStream(filename).getChannel();
      fileWriter.write(buffer);
    } finally {
      fileWriter.close();
    }

    if (!hideCommand.trim().equals("")) {
      // hideFile(filename);
    }
  }
 public static void main(String[] arguments) {
   try {
     // read byte data into a byte buffer
     String data = "friends.dat";
     FileInputStream inData = new FileInputStream(data);
     FileChannel inChannel = inData.getChannel();
     long inSize = inChannel.size();
     ByteBuffer source = ByteBuffer.allocate((int) inSize);
     inChannel.read(source, 0);
     source.position(0);
     System.out.println("Original byte data:");
     for (int i = 0; source.remaining() > 0; i++) {
       System.out.print(source.get() + " ");
     }
     // convert byte data into character data
     source.position(0);
     Charset ascii = Charset.forName("US-ASCII");
     CharsetDecoder toAscii = ascii.newDecoder();
     CharBuffer destination = toAscii.decode(source);
     destination.position(0);
     System.out.println("\n\nNew character data:");
     for (int i = 0; destination.remaining() > 0; i++) {
       System.out.print(destination.get());
     }
     System.out.println();
   } catch (FileNotFoundException fne) {
     System.out.println(fne.getMessage());
   } catch (IOException ioe) {
     System.out.println(ioe.getMessage());
   }
 }
  void isReadable(SelectionKey k) {
    EventableChannel ec = (EventableChannel) k.attachment();
    long b = ec.getBinding();

    if (ec.isWatchOnly()) {
      if (ec.isNotifyReadable()) eventCallback(b, EM_CONNECTION_NOTIFY_READABLE, null);
    } else {
      myReadBuffer.clear();

      try {
        ec.readInboundData(myReadBuffer);
        myReadBuffer.flip();
        if (myReadBuffer.limit() > 0) {
          if (ProxyConnections != null) {
            EventableChannel target = ProxyConnections.get(b);
            if (target != null) {
              ByteBuffer myWriteBuffer = ByteBuffer.allocate(myReadBuffer.limit());
              myWriteBuffer.put(myReadBuffer);
              myWriteBuffer.flip();
              target.scheduleOutboundData(myWriteBuffer);
            } else {
              eventCallback(b, EM_CONNECTION_READ, myReadBuffer);
            }
          } else {
            eventCallback(b, EM_CONNECTION_READ, myReadBuffer);
          }
        }
      } catch (IOException e) {
        UnboundConnections.add(b);
      }
    }
  }
  private boolean tagExists(FileChannel fc) throws IOException {
    ByteBuffer b = ByteBuffer.allocate(3);
    fc.position(0);
    fc.read(b);
    String tagString = new String(b.array());

    return tagString.equals("ID3");
  }
  public RandomAccessFile delete(RandomAccessFile raf, RandomAccessFile tempRaf)
      throws IOException {
    FileChannel fc = raf.getChannel();
    fc.position(0);

    if (!tagExists(fc)) return raf;

    fc.position(6);

    ByteBuffer b = ByteBuffer.allocate(4);
    fc.read(b);
    b.rewind();

    int tagSize = (b.get() & 0xFF) << 21;
    tagSize += (b.get() & 0xFF) << 14;
    tagSize += (b.get() & 0xFF) << 7;
    tagSize += b.get() & 0xFF;

    FileChannel tempFC = tempRaf.getChannel();
    tempFC.position(0);

    fc.position(tagSize + 10);

    // Here we will try to skip eventual trash afer the tag and before the
    // audio data
    b = ByteBuffer.allocate(4);
    int skip = 0;
    while (fc.read(b) != -1) {
      if ((b.get(0) & 0xFF) == 0xFF
          && (b.get(1) & 0xE0) == 0xE0
          && (b.get(1) & 0x06) != 0
          && (b.get(2) & 0xF0) != 0xF0
          && (b.get(2) & 0x08) != 0x08) {
        fc.position(fc.position() - 4);
        break;
      }

      fc.position(fc.position() - 3);
      b.rewind();
      skip++;
    }

    tempFC.transferFrom(fc, 0, fc.size() - tagSize - 10 - skip);
    return tempRaf;
  }
Exemple #12
0
  public static void runTests() {
    try {

      // SHA1 sha1Jmule = new SHA1();
      MessageDigest sha1Sun = MessageDigest.getInstance("SHA-1");
      SHA1 sha1Gudy = new SHA1();
      // SHA1Az shaGudyResume = new SHA1Az();

      ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);

      File dir = new File(dirname);
      File[] files = dir.listFiles();

      for (int i = 0; i < files.length; i++) {
        FileChannel fc = new RandomAccessFile(files[i], "r").getChannel();

        System.out.println("Testing " + files[i].getName() + " ...");

        while (fc.position() < fc.size()) {
          fc.read(buffer);
          buffer.flip();

          byte[] raw = new byte[buffer.limit()];
          System.arraycopy(buffer.array(), 0, raw, 0, raw.length);

          sha1Gudy.update(buffer);
          sha1Gudy.saveState();
          ByteBuffer bb = ByteBuffer.wrap(new byte[56081]);
          sha1Gudy.digest(bb);
          sha1Gudy.restoreState();

          sha1Sun.update(raw);

          buffer.clear();
        }

        byte[] sun = sha1Sun.digest();
        sha1Sun.reset();

        byte[] gudy = sha1Gudy.digest();
        sha1Gudy.reset();

        if (Arrays.equals(sun, gudy)) {
          System.out.println("  SHA1-Gudy: OK");
        } else {
          System.out.println("  SHA1-Gudy: FAILED");
        }

        buffer.clear();
        fc.close();
        System.out.println();
      }

    } catch (Throwable e) {
      Debug.printStackTrace(e);
    }
  }
 /*
  * Return a ByteBuffer with "remaining" space to work.  If you have to
  * reallocate the ByteBuffer, copy the existing info into the new buffer.
  */
 protected void resizeRequestBB(int remaining) {
   if (requestBB.remaining() < remaining) {
     // Expand buffer for large request
     ByteBuffer bb = ByteBuffer.allocate(requestBB.capacity() * 2);
     requestBB.flip();
     bb.put(requestBB);
     requestBB = bb;
   }
 }
  private void init() throws IOException {
    messageBuffer = new ArrayList();
    receiveBuffer = ByteBuffer.allocate(CAPACITY);
    sendBuffer = ByteBuffer.allocate(CAPACITY);
    buf = new byte[CAPACITY];

    channel = DatagramChannel.open();
    channel.configureBlocking(false);

    InetAddress host = null;
    if (getAddress() != null) {
      host = InetAddress.getByAddress(getAddress().getBytes());
    }
    channel.socket().bind(new InetSocketAddress(host, getPort()));
    channel.socket().setReceiveBufferSize(CAPACITY);

    certifier = new MessageCertifier(this);
    extractor = new MessageExtractor(this);
  }
  /*
   * Static factory method for creating a secure ChannelIO object.
   * <P>
   * We need to allocate different sized application data buffers
   * based on whether we're secure or not.  We can't determine
   * this until our sslEngine is created.
   */
  static ChannelIOSecure getInstance(SocketChannel sc, boolean blocking, SSLContext sslc)
      throws IOException {

    ChannelIOSecure cio = new ChannelIOSecure(sc, blocking, sslc);

    // Create a buffer using the normal expected application size we'll
    // be getting.  This may change, depending on the peer's
    // SSL implementation.
    cio.appBBSize = cio.sslEngine.getSession().getApplicationBufferSize();
    cio.requestBB = ByteBuffer.allocate(cio.appBBSize);

    return cio;
  }
Exemple #16
0
 public void write(FileOutputStream fos) throws IOException {
   FileChannel chan = fos.getChannel();
   // Create ByteBuffer for header in case the start of our
   // ByteBuffer isn't actually memory-mapped
   ByteBuffer hdr = ByteBuffer.allocate(Header.writtenSize());
   hdr.order(ByteOrder.LITTLE_ENDIAN);
   header.write(hdr);
   hdr.rewind();
   chan.write(hdr);
   buf.position(Header.writtenSize());
   chan.write(buf);
   chan.force(true);
   chan.close();
 }
  public static void main(String[] args) throws IOException {

    FileInputStream fin = new FileInputStream(args[0]);
    GZIPInputStream gzin = new GZIPInputStream(fin);
    ReadableByteChannel in = Channels.newChannel(gzin);

    WritableByteChannel out = Channels.newChannel(System.out);
    ByteBuffer buffer = ByteBuffer.allocate(65536);
    while (in.read(buffer) != -1) {
      buffer.flip();
      out.write(buffer);
      buffer.clear();
    }
  }
  public static void main(String[] args) throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    DatagramSocket socket = dc.socket();
    socket.bind(new InetSocketAddress(22500));
    ByteBuffer byteBuf = ByteBuffer.allocate(8);
    //        byteBuf.order(ByteOrder.LITTLE_ENDIAN);
    LongBuffer longBuf = byteBuf.asLongBuffer();

    while (true) {
      dc.receive(byteBuf);
      System.out.println(longBuf.get(0));
      byteBuf.clear();
    }
  }
  /*
   * Constructor for a secure ChannelIO variant.
   */
  protected ChannelIOSecure(SocketChannel sc, boolean blocking, SSLContext sslc)
      throws IOException {
    super(sc, blocking);

    /*
     * We're a server, so no need to use host/port variant.
     *
     * The first call for a server is a NEED_UNWRAP.
     */
    sslEngine = sslc.createSSLEngine();
    sslEngine.setUseClientMode(false);
    initialHSStatus = HandshakeStatus.NEED_UNWRAP;
    initialHSComplete = false;

    // Create a buffer using the normal expected packet size we'll
    // be getting.  This may change, depending on the peer's
    // SSL implementation.
    netBBSize = sslEngine.getSession().getPacketBufferSize();
    inNetBB = ByteBuffer.allocate(netBBSize);
    outNetBB = ByteBuffer.allocate(netBBSize);
    outNetBB.position(0);
    outNetBB.limit(0);
  }
  public static void main(String args[]) throws Exception {
    final int ENOUGH_SIZE = 1024;
    final int SMALL_SIZE = 4;

    boolean isBlocked = true;
    int size = ENOUGH_SIZE;

    if (args.length > 0) {
      int opt = Integer.parseInt(args[0]);
      switch (opt) {
        case 1:
          isBlocked = true;
          size = ENOUGH_SIZE;
          break;
        case 2:
          isBlocked = true;
          size = SMALL_SIZE;
          break;
        case 3:
          isBlocked = false;
          size = ENOUGH_SIZE;
          break;
        case 4:
          isBlocked = false;
          size = SMALL_SIZE;
          break;
      }
    }

    DatagramChannel channel = DatagramChannel.open();
    channel.configureBlocking(isBlocked);
    ByteBuffer buffer = ByteBuffer.allocate(size);
    DatagramSocket socket = channel.socket();
    SocketAddress localAddr = new InetSocketAddress(8000);
    socket.bind(localAddr);

    while (true) {
      System.out.println("开始接收数据报");
      SocketAddress remoteAddr = channel.receive(buffer);
      if (remoteAddr == null) {
        System.out.println("没有接收到数据报");
      } else {
        buffer.flip();
        System.out.println("接收到的数据报的大小为" + buffer.remaining());
      }
      Thread.sleep(500);
    }
  }
  public EmReactor() {
    Timers = new TreeMap<Long, ArrayList<Long>>();
    Connections = new HashMap<Long, EventableChannel>();
    Acceptors = new HashMap<Long, ServerSocketChannel>();
    NewConnections = new ArrayList<Long>();
    UnboundConnections = new ArrayList<Long>();
    DetachedConnections = new ArrayList<EventableSocketChannel>();

    BindingIndex = 0;
    loopBreaker = new AtomicBoolean();
    loopBreaker.set(false);
    myReadBuffer =
        ByteBuffer.allocate(
            32 * 1024); // don't use a direct buffer. Ruby doesn't seem to like them.
    timerQuantum = 98;
  }
  public static void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("readandshow.txt");
    FileChannel fc = fin.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(1024);

    fc.read(buffer);

    buffer.flip();

    int i = 0;
    while (buffer.remaining() > 0) {
      byte b = buffer.get();
      System.out.println("Character " + i + ": " + ((char) b));
      i++;
    }

    fin.close();
  }
  /*
   * TODO: Finish method.
   */
  public static String readBytesFromFile(File filename)
      throws IOException, OverlappingFileLockException {
    if (!filename.exists()) {
      return null;
    }

    try {
      ByteBuffer buffer = ByteBuffer.allocate(((int) filename.getTotalSpace() * 4));
      fileReader = new FileInputStream(filename).getChannel();
      FileLock lock = fileReader.tryLock();

      if (lock != null) {
        fileReader.read(buffer);
      } else {
        throw new OverlappingFileLockException();
      }
    } finally {
      fileWriter.close();
    }

    return "";
  }
  /**
   * Decode file charset.
   *
   * @param f File to process.
   * @return File charset.
   * @throws IOException in case of error.
   */
  public static Charset decode(File f) throws IOException {
    SortedMap<String, Charset> charsets = Charset.availableCharsets();

    String[] firstCharsets = {
      Charset.defaultCharset().name(), "US-ASCII", "UTF-8", "UTF-16BE", "UTF-16LE"
    };

    Collection<Charset> orderedCharsets = U.newLinkedHashSet(charsets.size());

    for (String c : firstCharsets)
      if (charsets.containsKey(c)) orderedCharsets.add(charsets.get(c));

    orderedCharsets.addAll(charsets.values());

    try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
      FileChannel ch = raf.getChannel();

      ByteBuffer buf = ByteBuffer.allocate(4096);

      ch.read(buf);

      buf.flip();

      for (Charset charset : orderedCharsets) {
        CharsetDecoder decoder = charset.newDecoder();

        decoder.reset();

        try {
          decoder.decode(buf);

          return charset;
        } catch (CharacterCodingException ignored) {
        }
      }
    }

    return Charset.defaultCharset();
  }
  public static void main(String args[]) {
    int count;
    Path filepath = null;

    // First, obtain a path to the file.
    try {
      filepath = Paths.get("test.txt");
    } catch (InvalidPathException e) {
      System.out.println("Path Error " + e);
      return;
    }

    // Next, obtain a channel to that file within a try-with-resources block.
    try (SeekableByteChannel fChan = Files.newByteChannel(filepath)) {

      // Allocate a buffer.
      ByteBuffer mBuf = ByteBuffer.allocate(128);

      do {
        // Read a buffer.
        count = fChan.read(mBuf);

        // Stop when end of file is reached.
        if (count != -1) {

          // Rewind the buffer so that it can be read.
          mBuf.rewind();

          // Read bytes from the buffer and show
          // them on the screen as characters.
          for (int i = 0; i < count; i++) System.out.print((char) mBuf.get());
        }
      } while (count != -1);

      System.out.println();
    } catch (IOException e) {
      System.out.println("I/O Error " + e);
    }
  }
  /*
   * Perform a FileChannel.TransferTo on the socket channel.
   * <P>
   * We have to copy the data into an intermediary app ByteBuffer
   * first, then send it through the SSLEngine.
   * <P>
   * We return the number of bytes actually read out of the
   * filechannel.  However, the data may actually be stuck
   * in the fileChannelBB or the outNetBB.  The caller
   * is responsible for making sure to call dataFlush()
   * before shutting down.
   */
  long transferTo(FileChannel fc, long pos, long len) throws IOException {

    if (!initialHSComplete) {
      throw new IllegalStateException();
    }

    if (fileChannelBB == null) {
      fileChannelBB = ByteBuffer.allocate(appBBSize);
      fileChannelBB.limit(0);
    }

    fileChannelBB.compact();
    int fileRead = fc.read(fileChannelBB);
    fileChannelBB.flip();

    /*
     * We ignore the return value here, we return the
     * number of bytes actually consumed from the the file.
     * We'll flush the output buffer before we start shutting down.
     */
    doWrite(fileChannelBB);

    return fileRead;
  }
public class MultiPortEcho {
  private int ports[];
  private ByteBuffer echoBuffer = ByteBuffer.allocate(1024);

  public MultiPortEcho(int ports[]) throws IOException {
    this.ports = ports;

    go();
  }

  private void go() throws IOException {
    // Create a new selector
    Selector selector = Selector.open();

    // Open a listener on each port, and register each one
    // with the selector
    for (int i = 0; i < ports.length; ++i) {
      ServerSocketChannel ssc = ServerSocketChannel.open();
      ssc.configureBlocking(false);
      ServerSocket ss = ssc.socket();
      InetSocketAddress address = new InetSocketAddress(ports[i]);
      ss.bind(address);

      SelectionKey key = ssc.register(selector, SelectionKey.OP_ACCEPT);

      System.out.println("Going to listen on " + ports[i]);
    }

    while (true) {
      int num = selector.select();

      Set selectedKeys = selector.selectedKeys();
      Iterator it = selectedKeys.iterator();

      while (it.hasNext()) {
        SelectionKey key = (SelectionKey) it.next();

        if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
          // Accept the new connection
          ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
          SocketChannel sc = ssc.accept();
          sc.configureBlocking(false);

          // Add the new connection to the selector
          SelectionKey newKey = sc.register(selector, SelectionKey.OP_READ);
          it.remove();

          System.out.println("Got connection from " + sc);
        } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
          // Read the data
          SocketChannel sc = (SocketChannel) key.channel();

          // Echo data
          int bytesEchoed = 0;
          while (true) {
            echoBuffer.clear();

            int r = sc.read(echoBuffer);

            if (r <= 0) {
              break;
            }

            echoBuffer.flip();

            sc.write(echoBuffer);
            bytesEchoed += r;
          }

          System.out.println("Echoed " + bytesEchoed + " from " + sc);

          it.remove();
        }
      }

      // System.out.println( "going to clear" );
      //      selectedKeys.clear();
      // System.out.println( "cleared" );
    }
  }

  public static void main(String args[]) throws Exception {
    int ports[] = new int[] {9001, 9002, 9003};

    for (int i = 0; i < args.length; ++i) {
      ports[i] = Integer.parseInt(args[i]);
    }

    new MultiPortEcho(ports);
  }
}
Exemple #28
0
  public static void main(String[] argv) {
    if (argv.length != 3) {
      usage();
    }

    String tempFile = argv[0];
    String testFile = argv[1];
    int fileSize = Integer.valueOf(argv[2]).intValue();
    int exitcode = 0;
    int numRead;
    int numThisBuf;
    int numWritten;

    if ((fileSize <= 0) || (fileSize % 4096 != 0)) {
      System.out.println("Error: size is not a multiple of 4096!!!!!!");
      System.out.println();
      usage();
    }

    try {
      int bufSize = 4096;
      byte[] inBytes = new byte[bufSize];
      byte[] outBytes = new byte[bufSize];
      Random ioRandom = new Random(2006);
      ioRandom.nextBytes(outBytes);
      ByteBuffer inBuf = ByteBuffer.allocate(bufSize);
      ByteBuffer outBuf = ByteBuffer.wrap(outBytes);

      //
      // Loop forever
      //
      while (true) {
        //
        // Write the temporary file
        //
        FileOutputStream fos = new FileOutputStream(tempFile);
        FileChannel foc = fos.getChannel();
        numWritten = 0;
        while (numWritten < fileSize) {
          outBuf.clear(); // sets limit to capacity & position to zero
          while (outBuf.hasRemaining()) {
            numWritten += foc.write(outBuf);
          }
        }

        //
        // Move to permanent location
        //
        FileChannel srcChannel = new FileInputStream(tempFile).getChannel();
        FileChannel dstChannel = new FileOutputStream(testFile).getChannel();
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        srcChannel.close();
        dstChannel.close();
        boolean success = (new File(tempFile)).delete();
        if (!success) {
          System.out.println("Warning: unable to delete temporary file");
        }

        //
        // Read and compare
        //
        FileInputStream fis = new FileInputStream(testFile);
        FileChannel fic = fis.getChannel();

        for (numRead = 0, numThisBuf = 0; numRead < fileSize; numThisBuf = 0) {
          inBuf.rewind(); // Set the buffer position to 0
          numThisBuf = fic.read(inBuf);
          while (numThisBuf < bufSize) {
            numThisBuf += fic.read(inBuf);
          }
          numRead += bufSize;
          inBuf.rewind(); // Set the buffer position to 0
          inBuf.get(inBytes);
          boolean same = Arrays.equals(inBytes, outBytes);
          if (same = false) {
            System.out.println("Data read does not equal data written at " + numRead + " bytes");
          }
        }
      }

    } catch (FileNotFoundException fnfe) {
      fnfe.printStackTrace(System.err);
      exitcode = 1;
      // break;
    } catch (SecurityException se) {
      se.printStackTrace(System.err);
      exitcode = 1;
      // break;
    } catch (Throwable t) {
      t.printStackTrace(System.err);
      exitcode = 1;
    }

    System.exit(exitcode);
  }
Exemple #29
0
  public static void main(String[] args) {
    try {
      System.out.println("CliTest.java");
      // create TCP socket channel
      SocketChannel channel = SocketChannel.open();
      System.out.println("CliTest.java: channel created");
      channel.connect(new InetSocketAddress("localhost", PORT));
      System.out.println("CliTest.java: channel socket connected");
      System.out.println();

      // create  & send a login message
      byte[] bytes = LoginMessage.getLoginMessage("user1", "password1");
      System.out.println("CliTest.java: login message created");
      System.out.println(LoginMessage.getMessageString(bytes));
      System.out.printf("CliTest.java: login message length in bytes: %d\n", bytes.length);
      ByteBuffer buf = ByteBuffer.allocate(4096);
      buf.put(bytes);
      buf.flip();
      // System.out.printf("CliTest.java: buf.remaining before channel.write(): %d\n",
      // buf.remaining());
      int numwritten = channel.write(buf);
      System.out.printf(
          "CliTest.java: login mesage written: number of bytes written: %d\n", numwritten);

      // read reply message
      buf.clear();
      int numread = channel.read(buf);
      bytes = new byte[numread];
      buf.flip();
      buf.get(bytes);
      if (LoginMessage.isLoginSuccessMessage(bytes)) {
        System.out.printf(
            "CliTest.java: first message read: Success Message: number of bytes read: %d\n",
            numread);
        // get remote port number from success message
        int port = LoginMessage.getPort(bytes);
        System.out.printf("Port Number: %d\n", port);
        byte playerid = LoginMessage.getPlayerId(bytes);
        System.out.printf("Player id: %d\n", playerid);
        String mcastString = LoginMessage.getMulticastAddress(bytes);
        System.out.printf("Multicast Address: %s\n", mcastString);
        // create datagram channel & connect to rem port
        DatagramChannel dchannel = DatagramChannel.open();
        dchannel.socket().bind(new InetSocketAddress(0));
        dchannel.socket().connect(new InetSocketAddress(channel.socket().getInetAddress(), port));
        // get localport of datagram socket
        int localport = dchannel.socket().getLocalPort();
        System.out.printf("UDP local port: %d\n", localport);
        // send success message to send port number to server
        bytes = LoginMessage.getLoginSuccessMessage(playerid, null, localport);
        buf.clear();
        buf.put(bytes);
        buf.flip();
        channel.write(buf);

        DeathMessage dm = new DeathMessage((byte) 1, (byte) 1);
        bytes = dm.getByteMessage();
        buf.clear();
        buf.put(bytes);
        buf.flip();
        channel.write(buf);
      } else {
        System.out.printf(
            "CliTest.java: first message read: NOT Success Message: number of bytes read: %d\n",
            numread);
      }
      channel.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
/**
 * A helper class which performs I/O using the SSLEngine API.
 *
 * <p>Each connection has a SocketChannel and a SSLEngine that is used through the lifetime of the
 * Channel. We allocate byte buffers for use as the outbound and inbound network buffers.
 *
 * <PRE>
 *               Application Data
 *               src      requestBB
 *                |           ^
 *                |     |     |
 *                v     |     |
 *           +----+-----|-----+----+
 *           |          |          |
 *           |       SSL|Engine    |
 *   wrap()  |          |          |  unwrap()
 *           | OUTBOUND | INBOUND  |
 *           |          |          |
 *           +----+-----|-----+----+
 *                |     |     ^
 *                |     |     |
 *                v           |
 *            outNetBB     inNetBB
 *                   Net data
 * </PRE>
 *
 * These buffers handle all of the intermediary data for the SSL connection. To make things easy,
 * we'll require outNetBB be completely flushed before trying to wrap any more data, but we could
 * certainly remove that restriction by using larger buffers.
 *
 * <p>There are many, many ways to handle compute and I/O strategies. What follows is a relatively
 * simple one. The reader is encouraged to develop the strategy that best fits the application.
 *
 * <p>In most of the non-blocking operations in this class, we let the Selector tell us when we're
 * ready to attempt an I/O operation (by the application repeatedly calling our methods). Another
 * option would be to attempt the operation and return from the method when no forward progress can
 * be made.
 *
 * <p>There's lots of room for enhancements and improvement in this example.
 *
 * <p>We're checking for SSL/TLS end-of-stream truncation attacks via sslEngine.closeInbound(). When
 * you reach the end of a input stream via a read() returning -1 or an IOException, we call
 * sslEngine.closeInbound() to signal to the sslEngine that no more input will be available. If the
 * peer's close_notify message has not yet been received, this could indicate a trucation attack, in
 * which an attacker is trying to prematurely close the connection. The closeInbound() will throw an
 * exception if this condition were present.
 *
 * @author Brad R. Wetmore
 * @author Mark Reinhold
 * @version %I%, %E%
 */
class ChannelIOSecure extends ChannelIO {

  private SSLEngine sslEngine = null;

  private int appBBSize;
  private int netBBSize;

  /*
   * All I/O goes through these buffers.
   * <P>
   * It might be nice to use a cache of ByteBuffers so we're
   * not alloc/dealloc'ing ByteBuffer's for each new SSLEngine.
   * <P>
   * We use our superclass' requestBB for our application input buffer.
   * Outbound application data is supplied to us by our callers.
   */
  private ByteBuffer inNetBB;
  private ByteBuffer outNetBB;

  /*
   * An empty ByteBuffer for use when one isn't available, say
   * as a source buffer during initial handshake wraps or for close
   * operations.
   */
  private static ByteBuffer hsBB = ByteBuffer.allocate(0);

  /*
   * The FileChannel we're currently transferTo'ing (reading).
   */
  private ByteBuffer fileChannelBB = null;

  /*
   * During our initial handshake, keep track of the next
   * SSLEngine operation that needs to occur:
   *
   *     NEED_WRAP/NEED_UNWRAP
   *
   * Once the initial handshake has completed, we can short circuit
   * handshake checks with initialHSComplete.
   */
  private HandshakeStatus initialHSStatus;
  private boolean initialHSComplete;

  /*
   * We have received the shutdown request by our caller, and have
   * closed our outbound side.
   */
  private boolean shutdown = false;

  /*
   * Constructor for a secure ChannelIO variant.
   */
  protected ChannelIOSecure(SocketChannel sc, boolean blocking, SSLContext sslc)
      throws IOException {
    super(sc, blocking);

    /*
     * We're a server, so no need to use host/port variant.
     *
     * The first call for a server is a NEED_UNWRAP.
     */
    sslEngine = sslc.createSSLEngine();
    sslEngine.setUseClientMode(false);
    initialHSStatus = HandshakeStatus.NEED_UNWRAP;
    initialHSComplete = false;

    // Create a buffer using the normal expected packet size we'll
    // be getting.  This may change, depending on the peer's
    // SSL implementation.
    netBBSize = sslEngine.getSession().getPacketBufferSize();
    inNetBB = ByteBuffer.allocate(netBBSize);
    outNetBB = ByteBuffer.allocate(netBBSize);
    outNetBB.position(0);
    outNetBB.limit(0);
  }

  /*
   * Static factory method for creating a secure ChannelIO object.
   * <P>
   * We need to allocate different sized application data buffers
   * based on whether we're secure or not.  We can't determine
   * this until our sslEngine is created.
   */
  static ChannelIOSecure getInstance(SocketChannel sc, boolean blocking, SSLContext sslc)
      throws IOException {

    ChannelIOSecure cio = new ChannelIOSecure(sc, blocking, sslc);

    // Create a buffer using the normal expected application size we'll
    // be getting.  This may change, depending on the peer's
    // SSL implementation.
    cio.appBBSize = cio.sslEngine.getSession().getApplicationBufferSize();
    cio.requestBB = ByteBuffer.allocate(cio.appBBSize);

    return cio;
  }

  /*
   * Calls up to the superclass to adjust the buffer size
   * by an appropriate increment.
   */
  protected void resizeRequestBB() {
    resizeRequestBB(appBBSize);
  }

  /*
   * Adjust the inbount network buffer to an appropriate size.
   */
  private void resizeResponseBB() {
    ByteBuffer bb = ByteBuffer.allocate(netBBSize);
    inNetBB.flip();
    bb.put(inNetBB);
    inNetBB = bb;
  }

  /*
   * Writes bb to the SocketChannel.
   * <P>
   * Returns true when the ByteBuffer has no remaining data.
   */
  private boolean tryFlush(ByteBuffer bb) throws IOException {
    super.write(bb);
    return !bb.hasRemaining();
  }

  /*
   * Perform any handshaking processing.
   * <P>
   * This variant is for Servers without SelectionKeys (e.g.
   * blocking).
   */
  boolean doHandshake() throws IOException {
    return doHandshake(null);
  }

  /*
   * Perform any handshaking processing.
   * <P>
   * If a SelectionKey is passed, register for selectable
   * operations.
   * <P>
   * In the blocking case, our caller will keep calling us until
   * we finish the handshake.  Our reads/writes will block as expected.
   * <P>
   * In the non-blocking case, we just received the selection notification
   * that this channel is ready for whatever the operation is, so give
   * it a try.
   * <P>
   * return:
   *		true when handshake is done.
   *		false while handshake is in progress
   */
  boolean doHandshake(SelectionKey sk) throws IOException {

    SSLEngineResult result;

    if (initialHSComplete) {
      return initialHSComplete;
    }

    /*
     * Flush out the outgoing buffer, if there's anything left in
     * it.
     */
    if (outNetBB.hasRemaining()) {

      if (!tryFlush(outNetBB)) {
        return false;
      }

      // See if we need to switch from write to read mode.

      switch (initialHSStatus) {

          /*
           * Is this the last buffer?
           */
        case FINISHED:
          initialHSComplete = true;
          // Fall-through to reregister need for a Read.

        case NEED_UNWRAP:
          if (sk != null) {
            sk.interestOps(SelectionKey.OP_READ);
          }
          break;
      }

      return initialHSComplete;
    }

    switch (initialHSStatus) {
      case NEED_UNWRAP:
        if (sc.read(inNetBB) == -1) {
          sslEngine.closeInbound();
          return initialHSComplete;
        }

        needIO:
        while (initialHSStatus == HandshakeStatus.NEED_UNWRAP) {
          resizeRequestBB(); // expected room for unwrap
          inNetBB.flip();
          result = sslEngine.unwrap(inNetBB, requestBB);
          inNetBB.compact();

          initialHSStatus = result.getHandshakeStatus();

          switch (result.getStatus()) {
            case OK:
              switch (initialHSStatus) {
                case NOT_HANDSHAKING:
                  throw new IOException("Not handshaking during initial handshake");

                case NEED_TASK:
                  initialHSStatus = doTasks();
                  break;

                case FINISHED:
                  initialHSComplete = true;
                  break needIO;
              }

              break;

            case BUFFER_UNDERFLOW:
              // Resize buffer if needed.
              netBBSize = sslEngine.getSession().getPacketBufferSize();
              if (netBBSize > inNetBB.capacity()) {
                resizeResponseBB();
              }

              /*
               * Need to go reread the Channel for more data.
               */
              if (sk != null) {
                sk.interestOps(SelectionKey.OP_READ);
              }
              break needIO;

            case BUFFER_OVERFLOW:
              // Reset the application buffer size.
              appBBSize = sslEngine.getSession().getApplicationBufferSize();
              break;

            default: // CLOSED:
              throw new IOException("Received" + result.getStatus() + "during initial handshaking");
          }
        } // "needIO" block.

        /*
         * Just transitioned from read to write.
         */
        if (initialHSStatus != HandshakeStatus.NEED_WRAP) {
          break;
        }

        // Fall through and fill the write buffers.

      case NEED_WRAP:
        /*
         * The flush above guarantees the out buffer to be empty
         */
        outNetBB.clear();
        result = sslEngine.wrap(hsBB, outNetBB);
        outNetBB.flip();

        initialHSStatus = result.getHandshakeStatus();

        switch (result.getStatus()) {
          case OK:
            if (initialHSStatus == HandshakeStatus.NEED_TASK) {
              initialHSStatus = doTasks();
            }

            if (sk != null) {
              sk.interestOps(SelectionKey.OP_WRITE);
            }

            break;

          default: // BUFFER_OVERFLOW/BUFFER_UNDERFLOW/CLOSED:
            throw new IOException("Received" + result.getStatus() + "during initial handshaking");
        }
        break;

      default: // NOT_HANDSHAKING/NEED_TASK/FINISHED
        throw new RuntimeException("Invalid Handshaking State" + initialHSStatus);
    } // switch

    return initialHSComplete;
  }

  /*
   * Do all the outstanding handshake tasks in the current Thread.
   */
  private SSLEngineResult.HandshakeStatus doTasks() {

    Runnable runnable;

    /*
     * We could run this in a separate thread, but
     * do in the current for now.
     */
    while ((runnable = sslEngine.getDelegatedTask()) != null) {
      runnable.run();
    }
    return sslEngine.getHandshakeStatus();
  }

  /*
   * Read the channel for more information, then unwrap the
   * (hopefully application) data we get.
   * <P>
   * If we run out of data, we'll return to our caller (possibly using
   * a Selector) to get notification that more is available.
   * <P>
   * Each call to this method will perform at most one underlying read().
   */
  int read() throws IOException {
    SSLEngineResult result;

    if (!initialHSComplete) {
      throw new IllegalStateException();
    }

    int pos = requestBB.position();

    if (sc.read(inNetBB) == -1) {
      sslEngine.closeInbound(); // probably throws exception
      return -1;
    }

    do {
      resizeRequestBB(); // expected room for unwrap
      inNetBB.flip();
      result = sslEngine.unwrap(inNetBB, requestBB);
      inNetBB.compact();

      /*
       * Could check here for a renegotation, but we're only
       * doing a simple read/write, and won't have enough state
       * transitions to do a complete handshake, so ignore that
       * possibility.
       */
      switch (result.getStatus()) {
        case BUFFER_OVERFLOW:
          // Reset the application buffer size.
          appBBSize = sslEngine.getSession().getApplicationBufferSize();
          break;

        case BUFFER_UNDERFLOW:
          // Resize buffer if needed.
          netBBSize = sslEngine.getSession().getPacketBufferSize();
          if (netBBSize > inNetBB.capacity()) {
            resizeResponseBB();

            break; // break, next read will support larger buffer.
          }
        case OK:
          if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
            doTasks();
          }
          break;

        default:
          throw new IOException("sslEngine error during data read: " + result.getStatus());
      }
    } while ((inNetBB.position() != 0) && result.getStatus() != Status.BUFFER_UNDERFLOW);

    return (requestBB.position() - pos);
  }

  /*
   * Try to write out as much as possible from the src buffer.
   */
  int write(ByteBuffer src) throws IOException {

    if (!initialHSComplete) {
      throw new IllegalStateException();
    }

    return doWrite(src);
  }

  /*
   * Try to flush out any existing outbound data, then try to wrap
   * anything new contained in the src buffer.
   * <P>
   * Return the number of bytes actually consumed from the buffer,
   * but the data may actually be still sitting in the output buffer,
   * waiting to be flushed.
   */
  private int doWrite(ByteBuffer src) throws IOException {
    int retValue = 0;

    if (outNetBB.hasRemaining() && !tryFlush(outNetBB)) {
      return retValue;
    }

    /*
     * The data buffer is empty, we can reuse the entire buffer.
     */
    outNetBB.clear();

    SSLEngineResult result = sslEngine.wrap(src, outNetBB);
    retValue = result.bytesConsumed();

    outNetBB.flip();

    switch (result.getStatus()) {
      case OK:
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
          doTasks();
        }
        break;

      default:
        throw new IOException("sslEngine error during data write: " + result.getStatus());
    }

    /*
     * Try to flush the data, regardless of whether or not
     * it's been selected.  Odds of a write buffer being full
     * is less than a read buffer being empty.
     */
    if (outNetBB.hasRemaining()) {
      tryFlush(outNetBB);
    }

    return retValue;
  }

  /*
   * Perform a FileChannel.TransferTo on the socket channel.
   * <P>
   * We have to copy the data into an intermediary app ByteBuffer
   * first, then send it through the SSLEngine.
   * <P>
   * We return the number of bytes actually read out of the
   * filechannel.  However, the data may actually be stuck
   * in the fileChannelBB or the outNetBB.  The caller
   * is responsible for making sure to call dataFlush()
   * before shutting down.
   */
  long transferTo(FileChannel fc, long pos, long len) throws IOException {

    if (!initialHSComplete) {
      throw new IllegalStateException();
    }

    if (fileChannelBB == null) {
      fileChannelBB = ByteBuffer.allocate(appBBSize);
      fileChannelBB.limit(0);
    }

    fileChannelBB.compact();
    int fileRead = fc.read(fileChannelBB);
    fileChannelBB.flip();

    /*
     * We ignore the return value here, we return the
     * number of bytes actually consumed from the the file.
     * We'll flush the output buffer before we start shutting down.
     */
    doWrite(fileChannelBB);

    return fileRead;
  }

  /*
   * Flush any remaining data.
   * <P>
   * Return true when the fileChannelBB and outNetBB are empty.
   */
  boolean dataFlush() throws IOException {
    boolean fileFlushed = true;

    if ((fileChannelBB != null) && fileChannelBB.hasRemaining()) {
      doWrite(fileChannelBB);
      fileFlushed = !fileChannelBB.hasRemaining();
    } else if (outNetBB.hasRemaining()) {
      tryFlush(outNetBB);
    }

    return (fileFlushed && !outNetBB.hasRemaining());
  }

  /*
   * Begin the shutdown process.
   * <P>
   * Close out the SSLEngine if not already done so, then
   * wrap our outgoing close_notify message and try to send it on.
   * <P>
   * Return true when we're done passing the shutdown messsages.
   */
  boolean shutdown() throws IOException {

    if (!shutdown) {
      sslEngine.closeOutbound();
      shutdown = true;
    }

    if (outNetBB.hasRemaining() && tryFlush(outNetBB)) {
      return false;
    }

    /*
     * By RFC 2616, we can "fire and forget" our close_notify
     * message, so that's what we'll do here.
     */
    outNetBB.clear();
    SSLEngineResult result = sslEngine.wrap(hsBB, outNetBB);
    if (result.getStatus() != Status.CLOSED) {
      throw new SSLException("Improper close state");
    }
    outNetBB.flip();

    /*
     * We won't wait for a select here, but if this doesn't work,
     * we'll cycle back through on the next select.
     */
    if (outNetBB.hasRemaining()) {
      tryFlush(outNetBB);
    }

    return (!outNetBB.hasRemaining() && (result.getHandshakeStatus() != HandshakeStatus.NEED_WRAP));
  }

  /*
   * close() is not overridden
   */
}