/** push bytes back, to be read again later. */
 private void pushback(byte[] bytes, int len) {
   if (pushbackBufferLen == 0) {
     pushbackBuffer = bytes; // TODO: copy?
     pushbackBufferLen = len;
     pushbackBufferOffset = 0;
   } else {
     final byte[] newPushbackBuffer = new byte[pushbackBufferLen + len];
     System.arraycopy(pushbackBuffer, 0, newPushbackBuffer, 0, pushbackBufferLen);
     System.arraycopy(bytes, 0, newPushbackBuffer, pushbackBufferLen, len);
     pushbackBuffer = newPushbackBuffer;
     pushbackBufferLen = pushbackBufferLen + len;
     pushbackBufferOffset = 0;
   }
 }
예제 #2
0
  /**
   * Gets a <tt>MultiplexedDatagramSocket</tt> which filters <tt>DatagramPacket</tt>s away from this
   * <tt>DatagramSocket</tt> using a specific <tt>DatagramPacketFilter</tt>. If such a
   * <tt>MultiplexedDatagramSocket</tt> does not exist in this instance, it is created.
   *
   * @param filter the <tt>DatagramPacketFilter</tt> to get a <tt>MultiplexedDatagramSocket</tt> for
   * @return a <tt>MultiplexedDatagramSocket</tt> which filters <tt>DatagramPacket</tt>s away from
   *     this <tt>DatagramSocket</tt> using the specified <tt>filter</tt>
   * @throws SocketException if creating the <tt>MultiplexedDatagramSocket</tt> for the specified
   *     <tt>filter</tt> fails
   */
  public MultiplexedDatagramSocket getSocket(DatagramPacketFilter filter) throws SocketException {
    if (filter == null) throw new NullPointerException("filter");

    synchronized (socketsSyncRoot) {
      /*
       * If a socket for the specified filter exists already, do not
       * create a new one and return the existing.
       */
      for (MultiplexedDatagramSocket socket : sockets)
        if (filter.equals(socket.getFilter())) return socket;

      // Create a new socket for the specified filter.
      MultiplexedDatagramSocket socket = new MultiplexedDatagramSocket(this, filter);

      // Remember the new socket.
      int socketCount = sockets.length;

      if (socketCount == 0) sockets = new MultiplexedDatagramSocket[] {socket};
      else {
        MultiplexedDatagramSocket[] newSockets = new MultiplexedDatagramSocket[socketCount + 1];

        System.arraycopy(sockets, 0, newSockets, 0, socketCount);
        newSockets[socketCount] = socket;
        sockets = newSockets;
      }

      return socket;
    }
  }
예제 #3
0
  /**
   * Copies the properties of a specific <tt>DatagramPacket</tt> to another <tt>DatagramPacket</tt>.
   * The property values are not cloned.
   *
   * @param src the <tt>DatagramPacket</tt> which is to have its properties copied to <tt>dest</tt>
   * @param dest the <tt>DatagramPacket</tt> which is to have its properties set to the value of the
   *     respective properties of <tt>src</tt>
   */
  public static void copy(DatagramPacket src, DatagramPacket dest) {
    synchronized (dest) {
      dest.setAddress(src.getAddress());
      dest.setPort(src.getPort());

      byte[] srcData = src.getData();

      if (srcData == null) dest.setLength(0);
      else {
        byte[] destData = dest.getData();

        if (destData == null) dest.setLength(0);
        else {
          int destOffset = dest.getOffset();
          int destLength = destData.length - destOffset;
          int srcLength = src.getLength();

          if (destLength >= srcLength) destLength = srcLength;
          else if (logger.isLoggable(Level.WARNING)) {
            logger.log(Level.WARNING, "Truncating received DatagramPacket data!");
          }
          System.arraycopy(srcData, src.getOffset(), destData, destOffset, destLength);
          dest.setLength(destLength);
        }
      }
    }
  }
예제 #4
0
  /**
   * Closes a specific <tt>MultiplexedDatagramSocket</tt> which filters <tt>DatagramPacket</tt>s
   * away from this <tt>DatagramSocket</tt>.
   *
   * @param multiplexed the <tt>MultiplexedDatagramSocket</tt> to close
   */
  void close(MultiplexedDatagramSocket multiplexed) {
    synchronized (socketsSyncRoot) {
      int socketCount = sockets.length;

      for (int i = 0; i < socketCount; i++)
        if (sockets[i].equals(multiplexed)) {
          if (socketCount == 1) sockets = NO_SOCKETS;
          else {
            MultiplexedDatagramSocket[] newSockets = new MultiplexedDatagramSocket[socketCount - 1];

            System.arraycopy(sockets, 0, newSockets, 0, i);
            System.arraycopy(sockets, i + 1, newSockets, i, newSockets.length - i);
            sockets = newSockets;
          }
          break;
        }
    }
  }
 /** supports pushback. */
 private int read(byte[] buffer, int offset, int length) throws IOException {
   if (pushbackBufferLen > 0) { // read from pushback buffer
     final int lenToCopy = length < pushbackBufferLen ? length : pushbackBufferLen;
     System.arraycopy(pushbackBuffer, pushbackBufferOffset, buffer, offset, lenToCopy);
     pushbackBufferLen -= lenToCopy;
     pushbackBufferOffset += lenToCopy;
     return lenToCopy;
   } else {
     return stream.read(buffer, offset, length);
   }
 }
예제 #6
0
  protected static Object[] extractMockArguments(Object[] args) {
    int i = 7;

    if (args.length > i) {
      Object[] mockArgs = new Object[args.length - i];
      System.arraycopy(args, i, mockArgs, 0, mockArgs.length);
      return mockArgs;
    }

    return EMPTY_ARGS;
  }
예제 #7
0
  private void stripOperandFromArgs() {
    // remove the domain-name operand
    // it may not be here!
    if (args.length < 2 || !StringUtils.ok(serverName)) return;

    int newlen = args.length - 1;

    if (serverName.equals(args[newlen])) {
      String[] newargs = new String[newlen];
      System.arraycopy(args, 0, newargs, 0, newlen);
      args = newargs;
    }
  }
  // @Override
  @Override
  public int read(byte[] b, int off, int len) throws IOException {
    // TODO: how do we detect IOException?
    fillBuffer();
    if (buffer.getLength() == 0 && buffer.isEOM()) // TODO: will always be
      // EOM if length is 0
      return -1;
    final byte[] data = (byte[]) buffer.getData();

    int lengthToCopy = buffer.getLength() < len ? buffer.getLength() : len;
    System.arraycopy(data, buffer.getOffset(), b, off, lengthToCopy);
    buffer.setOffset(buffer.getOffset() + lengthToCopy);
    buffer.setLength(buffer.getLength() - lengthToCopy);

    return lengthToCopy;
  }
예제 #9
0
  private void handleDebug() {
    if (debug == null) // nothing to do!
    return;

    stripDebugFromArgs();
    stripOperandFromArgs();
    int oldlen = args.length;
    int newlen = oldlen + 2;
    String debugArg = "--debug=" + debug.toString();
    String[] newArgs = new String[newlen];

    // copy all but the last arg (domain-name)
    System.arraycopy(args, 0, newArgs, 0, args.length);
    newArgs[newlen - 2] = debugArg;
    newArgs[newlen - 1] = serverName;
    args = newArgs;
  }
예제 #10
0
  public boolean handleCommand(Player player, String[] split) {

    try {

      split[0] = split[0].substring(1);

      // Quick script shortcut
      if (split[0].matches("^[^/].*\\.js$")) {
        String[] newSplit = new String[split.length + 1];
        System.arraycopy(split, 0, newSplit, 1, split.length);
        newSplit[0] = "cs";
        newSplit[1] = newSplit[1];
        split = newSplit;
      }

      // No command found!
      if (!commandMap.hasCommand(split[0])) {
        return false;
      }

      try {
        commandMap.execute(split, player, this, player);
      } catch (CommandPermissionsException e) {
        player.sendMessage("You don't have permission to do this.");
      } catch (MissingNestedCommandException e) {
        player.sendMessage(e.getUsage());
      } catch (CommandUsageException e) {
        player.sendMessage(e.getMessage());
        player.sendMessage(e.getUsage());
      } catch (WrappedCommandException e) {
        throw e.getCause();
      } catch (UnhandledCommandException e) {
        return false;
      } finally {

      }

    } catch (Throwable excp) {

      player.sendMessage("Please report this error:");
      player.sendMessage(excp.getMessage());
    }

    return true;
  }
예제 #11
0
 public ConstantMatrix cloneMatrixZeroed() {
   assert (values != null);
   int[] newIndices = new int[indices.length];
   System.arraycopy(indices, 0, newIndices, 0, indices.length);
   IndexedSparseVector sv =
       new IndexedSparseVector(
           newIndices,
           new double[values.length],
           values.length,
           values.length,
           false,
           false,
           false);
   // Share the index2location array.  This will be unsafe if
   // IndexedSparseVectors are ever allowed to be modifiable, but I
   // don't think that this will be the case.
   if (index2location != null) sv.index2location = index2location;
   return sv;
 }
예제 #12
0
  /**
   * @param in input stream to be used (e.g. System.in)
   * @param prompt The prompt to display to the user.
   * @return The password as entered by the user.
   */
  public static final char[] getPassword(InputStream in, String prompt) throws IOException {
    MaskingThread maskingthread = new MaskingThread(prompt);
    Thread thread = new Thread(maskingthread);
    thread.start();

    char[] lineBuffer;
    char[] buf;
    int i;

    buf = lineBuffer = new char[128];

    int room = buf.length;
    int offset = 0;
    int c;

    loop:
    while (true) {
      c = in.read();
      switch (c) {
        case -1:
        case '\n':
          break loop;

        case '\r':
          int c2 = in.read();
          if ((c2 != '\n') && (c2 != -1)) {
            if (!(in instanceof PushbackInputStream)) {
              in = new PushbackInputStream(in);
            }
            ((PushbackInputStream) in).unread(c2);
          } else {
            break loop;
          }
        default:
          if (--room < 0) {
            buf = new char[offset + 128];
            room = buf.length - offset - 1;
            System.arraycopy(lineBuffer, 0, buf, 0, offset);
            Arrays.fill(lineBuffer, ' ');
            lineBuffer = buf;
          }
          buf[offset++] = (char) c;
          break;
      }
    }
    maskingthread.stopMasking();
    System.out.print("\010");
    // Code to clear doskey on win nt/2000 - Alt+F7
    String os = System.getProperty("os.name");
    if (os != null && os.toLowerCase().startsWith("windows")) {
      try {
        java.awt.Robot robot = new java.awt.Robot();
        robot.keyPress(java.awt.event.KeyEvent.VK_ALT);
        robot.keyPress(java.awt.event.KeyEvent.VK_F7);
        robot.keyRelease(java.awt.event.KeyEvent.VK_F7);
        robot.keyRelease(java.awt.event.KeyEvent.VK_ALT);
      } catch (Exception ignore) {
        logger.warning("Could not clears command history: " + ignore);
      }
    }

    if (offset == 0) {
      return null;
    }
    char[] ret = new char[offset];
    System.arraycopy(buf, 0, ret, 0, offset);
    Arrays.fill(buf, ' ');
    return ret;
  }