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");
  }
Exemple #2
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);
    }
  }
 private void dispatchMessage(
     TcpAddress incomingAddress, ByteBuffer byteBuffer, long bytesRead) {
   byteBuffer.flip();
   if (logger.isDebugEnabled()) {
     logger.debug(
         "Received message from "
             + incomingAddress
             + " with length "
             + bytesRead
             + ": "
             + new OctetString(byteBuffer.array(), 0, (int) bytesRead).toHexString());
   }
   ByteBuffer bis;
   if (isAsyncMsgProcessingSupported()) {
     byte[] bytes = new byte[(int) bytesRead];
     System.arraycopy(byteBuffer.array(), 0, bytes, 0, (int) bytesRead);
     bis = ByteBuffer.wrap(bytes);
   } else {
     bis = ByteBuffer.wrap(byteBuffer.array(), 0, (int) bytesRead);
   }
   fireProcessMessage(incomingAddress, bis);
 }
Exemple #4
0
 private int _read(ByteBuffer buf) throws IOException {
   int x = _in.read(buf.array(), buf.position(), buf.remaining());
   if (x < 0) throw new IOException("connection to server closed unexpectedly");
   buf.position(buf.position() + x);
   return x;
 }