public void handleTransaction(Packet106Transaction par1Packet106Transaction) {
    Short short1 = (Short) field_72586_s.lookup(playerEntity.craftingInventory.windowId);

    if (short1 != null
        && par1Packet106Transaction.shortWindowId == short1.shortValue()
        && playerEntity.craftingInventory.windowId == par1Packet106Transaction.windowId
        && !playerEntity.craftingInventory.isPlayerNotUsingContainer(playerEntity)) {
      playerEntity.craftingInventory.setPlayerIsPresent(playerEntity, true);
    }
  }
示例#2
0
 /**
  * Create and open a Connection. This Implementation only supports the <code>btl2cap://</code>
  * protocol schema. If url begins with <code>btl2cap://localhost</code>, this method call will
  * return a <code>javax.bluetooth.L2CAPConnectionNotifier</code>.
  *
  * @param url The URL for the connection.
  * @return A new <code>javax.bluetooth.L2CAPConnection</code> or a <code>
  *     javax.bluetooth.L2CAPConnectionNotifier</code>
  * @throws IllegalArgumentException If a parameter is invalid.
  * @throws IOException If some other kind of I/O error occurs.
  * @see javax.bluetooth.L2CAPConnection
  * @see javax.bluetooth.L2CAPConnectionNotifier
  */
 public static Connection open(String url) throws IOException {
   try {
     if (url.startsWith("btl2cap://localhost:")) {
       int endIndex = url.indexOf(';');
       String psmString = url.substring(20, endIndex);
       Short psmShort = Short.decode(psmString);
       short psm = psmShort.shortValue();
       return new JSR82ConnectionNotifier(psm);
     }
     if (url.startsWith("btl2cap://")) {
       byte[] bdAddrBytes = new byte[12];
       String bdAddrString = url.substring(10, 22);
       Long bdAddrLong = Long.decode("0x" + bdAddrString);
       long remoteAddrLong = bdAddrLong.longValue();
       int endIndex = url.indexOf(';', 22);
       String psmString = url.substring(23, endIndex);
       Short psmShort = Short.decode(psmString);
       short psm = psmShort.shortValue();
       BluetoothStack bluetooth = BluetoothStack.getBluetoothStack();
       LocalDevice localDev = LocalDevice.getLocalDevice();
       DiscoveryAgent discovery = localDev.getDiscoveryAgent();
       RemoteDevice remoteDevice = discovery.getRemoteDevice(remoteAddrLong);
       if (remoteDevice != null) {
         JSR82Channel channel = new JSR82Channel();
         bluetooth.connectL2CAPChannel(channel, remoteDevice, psm);
         return channel;
       } else throw new IllegalArgumentException("Unable to locate Bluetooth Device.");
     }
   } catch (BluetoothStateException e) {
     throw new IOException("" + e);
   } catch (HCIException e) {
     throw new IOException("" + e);
   }
   throw new IllegalArgumentException(
       "This implementation of Connector only supports btl2cap:// Connections.");
 }
示例#3
0
 public short getAddress(String functionName) throws ProgramException {
   Short address = (Short) functions.get(functionName);
   if (address != null) {
     return address.shortValue();
   } else {
     String className = functionName.substring(0, functionName.indexOf("."));
     if (staticRange.get(className) == null) {
       // The class is not implemented by a VM file - search for a
       // built-in implementation later. Display a popup to confirm
       // this as this is not a feature from the book but a later
       // addition.
       if (builtInAccessStatus == BUILTIN_ACCESS_UNDECIDED) {
         if (hasGUI && gui.confirmBuiltInAccess()) {
           builtInAccessStatus = BUILTIN_ACCESS_AUTHORIZED;
         } else {
           builtInAccessStatus = BUILTIN_ACCESS_DENIED;
         }
       }
       if (builtInAccessStatus == BUILTIN_ACCESS_AUTHORIZED) {
         return BUILTIN_FUNCTION_ADDRESS;
       }
     }
     // Either:
     // 1.The class is implemented by a VM file and no implementation
     //     for the function is found - don't override with built-in
     // - or -
     // 2.The user did not authorize using built-in implementations.
     throw new ProgramException(
         className
             + ".vm not found "
             + "or function "
             + functionName
             + " not found in "
             + className
             + ".vm");
   }
 }
  /**
   * Create a new reader.
   *
   * @param in the input
   * @param out the output
   * @param bindings the key bindings to use
   * @param term the terminal to use
   */
  public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
      throws IOException {
    this.terminal = term;
    setInput(in);
    this.out = out;

    if (bindings == null) {
      try {
        String bindingFile =
            System.getProperty(
                "jline.keybindings",
                new File(System.getProperty("user.home", ".jlinebindings.properties"))
                    .getAbsolutePath());

        if (new File(bindingFile).isFile()) {
          bindings = new FileInputStream(new File(bindingFile));
        }
      } catch (Exception e) {
        // swallow exceptions with option debugging
        if (debugger != null) {
          e.printStackTrace(debugger);
        }
      }
    }

    if (bindings == null) {
      bindings = terminal.getDefaultBindings();
    }

    this.keybindings = new short[Character.MAX_VALUE * 2];

    Arrays.fill(this.keybindings, UNKNOWN);

    /**
     * Loads the key bindings. Bindings file is in the format:
     *
     * <p>keycode: operation name
     */
    if (bindings != null) {
      Properties p = new Properties();
      p.load(bindings);
      bindings.close();

      for (Iterator i = p.keySet().iterator(); i.hasNext(); ) {
        String val = (String) i.next();

        try {
          Short code = new Short(val);
          String op = (String) p.getProperty(val);

          Short opval = (Short) KEYMAP_NAMES.get(op);

          if (opval != null) {
            keybindings[code.shortValue()] = opval.shortValue();
          }
        } catch (NumberFormatException nfe) {
          consumeException(nfe);
        }
      }

      // hardwired arrow key bindings
      // keybindings[VK_UP] = PREV_HISTORY;
      // keybindings[VK_DOWN] = NEXT_HISTORY;
      // keybindings[VK_LEFT] = PREV_CHAR;
      // keybindings[VK_RIGHT] = NEXT_CHAR;
    }
  }
示例#5
0
  public void writeBytes(OutputStream os) throws IOException {
    // Map between any modified UTF-8 and it's constant pool index.
    Map utf8ToIndex = new HashMap();
    DataOutputStream dos = new DataOutputStream(os);
    TypeArray tags = getTags();
    int len = (int) getLength();
    int ci = 0; // constant pool index

    // collect all modified UTF-8 Strings from Constant Pool

    for (ci = 1; ci < len; ci++) {
      byte cpConstType = tags.getByteAt(ci);
      if (cpConstType == JVM_CONSTANT_Utf8) {
        Symbol sym = getSymbolAt(ci);
        utf8ToIndex.put(sym.asString(), new Short((short) ci));
      } else if (cpConstType == JVM_CONSTANT_Long || cpConstType == JVM_CONSTANT_Double) {
        ci++;
      }
    }

    for (ci = 1; ci < len; ci++) {
      int cpConstType = (int) tags.getByteAt(ci);
      // write cp_info
      // write constant type
      switch (cpConstType) {
        case JVM_CONSTANT_Utf8:
          {
            dos.writeByte(cpConstType);
            Symbol sym = getSymbolAt(ci);
            dos.writeShort((short) sym.getLength());
            dos.write(sym.asByteArray());
            if (DEBUG) debugMessage("CP[" + ci + "] = modified UTF-8 " + sym.asString());
            break;
          }

        case JVM_CONSTANT_Unicode:
          throw new IllegalArgumentException("Unicode constant!");

        case JVM_CONSTANT_Integer:
          dos.writeByte(cpConstType);
          dos.writeInt(getIntAt(ci));
          if (DEBUG) debugMessage("CP[" + ci + "] = int " + getIntAt(ci));
          break;

        case JVM_CONSTANT_Float:
          dos.writeByte(cpConstType);
          dos.writeFloat(getFloatAt(ci));
          if (DEBUG) debugMessage("CP[" + ci + "] = float " + getFloatAt(ci));
          break;

        case JVM_CONSTANT_Long:
          {
            dos.writeByte(cpConstType);
            long l = getLongAt(ci);
            // long entries occupy two pool entries
            ci++;
            dos.writeLong(l);
            break;
          }

        case JVM_CONSTANT_Double:
          dos.writeByte(cpConstType);
          dos.writeDouble(getDoubleAt(ci));
          // double entries occupy two pool entries
          ci++;
          break;

        case JVM_CONSTANT_Class:
          {
            dos.writeByte(cpConstType);
            // Klass already resolved. ConstantPool constains klassOop.
            Klass refKls = (Klass) getObjAt(ci);
            String klassName = refKls.getName().asString();
            Short s = (Short) utf8ToIndex.get(klassName);
            dos.writeShort(s.shortValue());
            if (DEBUG) debugMessage("CP[" + ci + "] = class " + s);
            break;
          }

          // case JVM_CONSTANT_ClassIndex:
        case JVM_CONSTANT_UnresolvedClass:
          {
            dos.writeByte(JVM_CONSTANT_Class);
            String klassName = getSymbolAt(ci).asString();
            Short s = (Short) utf8ToIndex.get(klassName);
            dos.writeShort(s.shortValue());
            if (DEBUG) debugMessage("CP[" + ci + "] = class " + s);
            break;
          }

        case JVM_CONSTANT_String:
          {
            dos.writeByte(cpConstType);
            String str = OopUtilities.stringOopToString(getObjAt(ci));
            Short s = (Short) utf8ToIndex.get(str);
            dos.writeShort(s.shortValue());
            if (DEBUG) debugMessage("CP[" + ci + "] = string " + s);
            break;
          }

          // case JVM_CONSTANT_StringIndex:
        case JVM_CONSTANT_UnresolvedString:
          {
            dos.writeByte(JVM_CONSTANT_String);
            String val = getSymbolAt(ci).asString();

            Short s = (Short) utf8ToIndex.get(val);
            dos.writeShort(s.shortValue());
            if (DEBUG) debugMessage("CP[" + ci + "] = string " + s);
            break;
          }

          // all external, internal method/field references
        case JVM_CONSTANT_Fieldref:
        case JVM_CONSTANT_Methodref:
        case JVM_CONSTANT_InterfaceMethodref:
          {
            dos.writeByte(cpConstType);
            int value = getIntAt(ci);
            short klassIndex = (short) extractLowShortFromInt(value);
            short nameAndTypeIndex = (short) extractHighShortFromInt(value);
            dos.writeShort(klassIndex);
            dos.writeShort(nameAndTypeIndex);
            if (DEBUG)
              debugMessage(
                  "CP[" + ci + "] = ref klass = " + klassIndex + ", N&T = " + nameAndTypeIndex);
            break;
          }

        case JVM_CONSTANT_NameAndType:
          {
            dos.writeByte(cpConstType);
            int value = getIntAt(ci);
            short nameIndex = (short) extractLowShortFromInt(value);
            short signatureIndex = (short) extractHighShortFromInt(value);
            dos.writeShort(nameIndex);
            dos.writeShort(signatureIndex);
            if (DEBUG)
              debugMessage(
                  "CP[" + ci + "] = N&T name = " + nameIndex + ", type = " + signatureIndex);
            break;
          }
      } // switch
    }
    dos.flush();
    return;
  }
 private static Short negate(Short v) {
   return new Short((short) -v.shortValue());
 }
示例#7
0
  /**
   * Creates a vm program. If the given file is a dir, creates a program composed of the vm files in
   * the dir. The vm files are scanned twice: in the first scan a symbol table (that maps function &
   * label names into addresses) is built. In the second scan, the instructions array is built.
   * Throws ProgramException if an error occurs while loading the program.
   */
  public void loadProgram(String fileName) throws ProgramException {
    File file = new File(fileName);
    if (!file.exists()) throw new ProgramException("cannot find " + fileName);

    File[] files;

    if (file.isDirectory()) {
      files = file.listFiles(new HackFileFilter(".vm"));
      if (files == null || files.length == 0)
        throw new ProgramException("No vm files found in " + fileName);
    } else files = new File[] {file};

    if (displayChanges) gui.showMessage("Loading...");

    // First scan
    staticRange.clear();
    functions.clear();
    builtInAccessStatus = BUILTIN_ACCESS_UNDECIDED;
    Hashtable symbols = new Hashtable();
    nextPC = 0;
    for (int i = 0; i < files.length; i++) {
      String name = files[i].getName();
      String className = name.substring(0, name.indexOf("."));
      // put some dummy into static range - just to tell the function
      // getAddress in the second pass which classes exist
      staticRange.put(className, new Boolean(true));
      try {
        updateSymbolTable(files[i], symbols, functions);
      } catch (ProgramException pe) {
        if (displayChanges) gui.hideMessage();
        throw new ProgramException(name + ": " + pe.getMessage());
      }
    }
    boolean addCallBuiltInSysInit = false;
    if ((file.isDirectory() || symbols.get("Main.main") != null)
        && symbols.get("Sys.init") == null) {
      // If the program is in multiple files or there's a Main.main
      // function it is assumed that it should be run by calling Sys.init.
      // If no Sys.init is found, add an invisible line with a call
      // to Sys.init to start on - the builtin version will be called.
      addCallBuiltInSysInit = true;
      getAddress("Sys.init"); // confirm calling the built-in Sys.init
      ++nextPC; // A "call Sys.init 0" line will be added
    }

    instructions = new VMEmulatorInstruction[nextPC + 4];

    // Second scan
    nextPC = 0;
    currentStaticIndex = Definitions.VAR_START_ADDRESS;
    for (int i = 0; i < files.length; i++) {
      String name = files[i].getName();
      String className = name.substring(0, name.indexOf("."));

      largestStaticIndex = -1;
      int[] range = new int[2];
      range[0] = currentStaticIndex;

      try {
        // functions is not passed as an argument since it is accessed
        // through getAddress()
        buildProgram(files[i], symbols);
      } catch (ProgramException pe) {
        if (displayChanges) gui.hideMessage();
        throw new ProgramException(name + ": " + pe.getMessage());
      }

      currentStaticIndex += largestStaticIndex + 1;
      range[1] = currentStaticIndex - 1;
      staticRange.put(className, range);
    }
    instructionsLength = visibleInstructionsLength = nextPC;
    if (builtInAccessStatus == BUILTIN_ACCESS_AUTHORIZED) {
      // Add some "invisible" code in the end to make everything work
      instructionsLength += 4;
      if (addCallBuiltInSysInit) {
        instructionsLength += 1;
      }
      short indexInInvisibleCode = 0;
      // Add a jump to the end (noone should get here since
      // both calls to built-in functions indicate that
      // that this is a function-based program and not a script
      // a-la proj7, but just to be on the safe side...).
      instructions[nextPC] =
          new VMEmulatorInstruction(
              HVMInstructionSet.GOTO_CODE, (short) instructionsLength, indexInInvisibleCode);
      instructions[nextPC].setStringArg("afterInvisibleCode");
      nextPC++;
      // Add a small infinite loop for built-in
      // methods to call (for example when Sys.halt is
      // called it must call a non-built-in infinite loop
      // because otherwise the current script would not
      // finish running - a problem for the OS tests.
      instructions[nextPC] = new VMEmulatorInstruction(HVMInstructionSet.LABEL_CODE, (short) -1);
      instructions[nextPC].setStringArg("infiniteLoopForBuiltIns");
      nextPC++;
      infiniteLoopForBuiltInsAddress = nextPC;
      instructions[nextPC] =
          new VMEmulatorInstruction(HVMInstructionSet.GOTO_CODE, nextPC, ++indexInInvisibleCode);
      instructions[nextPC].setStringArg("infiniteLoopForBuiltIns");
      nextPC++;
      if (addCallBuiltInSysInit) { // Add a call to the built-in Sys.init
        instructions[nextPC] =
            new VMEmulatorInstruction(
                HVMInstructionSet.CALL_CODE,
                getAddress("Sys.init"),
                (short) 0,
                ++indexInInvisibleCode);
        instructions[nextPC].setStringArg("Sys.init");
        startAddress = nextPC;
        nextPC++;
      }
      // Add the label that the first invisible code line jumps to
      instructions[nextPC] = new VMEmulatorInstruction(HVMInstructionSet.LABEL_CODE, (short) -1);
      instructions[nextPC].setStringArg("afterInvisibleCode");
      nextPC++;
    }

    if (!addCallBuiltInSysInit) {
      Short sysInitAddress = (Short) symbols.get("Sys.init");
      if (sysInitAddress == null) // Single file, no Sys.init - start at 0
      startAddress = 0;
      else // Implemented Sys.init - start there
      startAddress = sysInitAddress.shortValue();
    }

    if (displayChanges) gui.hideMessage();

    nextPC = startAddress;
    setGUIContents();

    notifyProgramListeners(ProgramEvent.LOAD, fileName);
  }
示例#8
0
  // Scans the given file and creates symbols for its functions & label names.
  private void buildProgram(File file, Hashtable symbols) throws ProgramException {

    BufferedReader reader = null;

    try {
      reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
    } catch (FileNotFoundException fnfe) {
      throw new ProgramException("file does not exist");
    }

    int lineNumber = 0;
    String line;
    String label;
    String instructionName;
    String currentFunction = null;
    short indexInFunction = 0;
    byte opCode;
    short arg0, arg1;
    short pc = nextPC;
    HVMInstructionSet instructionSet = HVMInstructionSet.getInstance();

    isSlashStar = false;
    try {
      while ((line = unCommentLine(reader.readLine())) != null) {
        lineNumber++;

        if (!line.trim().equals("")) {
          StringTokenizer tokenizer = new StringTokenizer(line);
          instructionName = tokenizer.nextToken();

          opCode = instructionSet.instructionStringToCode(instructionName);
          if (opCode == HVMInstructionSet.UNKNOWN_INSTRUCTION)
            throw new ProgramException(
                "in line " + lineNumber + ": unknown instruction - " + instructionName);

          switch (opCode) {
            case HVMInstructionSet.PUSH_CODE:
              String segment = tokenizer.nextToken();
              try {
                arg0 = translateSegment(segment, instructionSet, file.getName());
              } catch (ProgramException pe) {
                throw new ProgramException("in line " + lineNumber + pe.getMessage());
              }
              arg1 = Short.parseShort(tokenizer.nextToken());
              if (arg1 < 0)
                throw new ProgramException(
                    "in line " + lineNumber + ": Illegal argument - " + line);

              if (arg0 == HVMInstructionSet.STATIC_SEGMENT_CODE && arg1 > largestStaticIndex)
                largestStaticIndex = arg1;

              instructions[pc] = new VMEmulatorInstruction(opCode, arg0, arg1, indexInFunction);
              break;

            case HVMInstructionSet.POP_CODE:
              int n = tokenizer.countTokens();
              segment = tokenizer.nextToken();
              try {
                arg0 = translateSegment(segment, instructionSet, file.getName());
              } catch (ProgramException pe) {
                throw new ProgramException("in line " + lineNumber + pe.getMessage());
              }
              arg1 = Short.parseShort(tokenizer.nextToken());

              if (arg1 < 0)
                throw new ProgramException(
                    "in line " + lineNumber + ": Illegal argument - " + line);

              if (arg0 == HVMInstructionSet.STATIC_SEGMENT_CODE && arg1 > largestStaticIndex)
                largestStaticIndex = arg1;

              instructions[pc] = new VMEmulatorInstruction(opCode, arg0, arg1, indexInFunction);
              break;

            case HVMInstructionSet.FUNCTION_CODE:
              currentFunction = tokenizer.nextToken();
              indexInFunction = 0;
              arg0 = Short.parseShort(tokenizer.nextToken());

              if (arg0 < 0)
                throw new ProgramException(
                    "in line " + lineNumber + ": Illegal argument - " + line);

              instructions[pc] = new VMEmulatorInstruction(opCode, arg0, indexInFunction);
              instructions[pc].setStringArg(currentFunction);
              break;

            case HVMInstructionSet.CALL_CODE:
              String functionName = tokenizer.nextToken();
              try {
                arg0 = getAddress(functionName);
              } catch (ProgramException pe) {
                throw new ProgramException("in line " + lineNumber + ": " + pe.getMessage());
              }
              arg1 = Short.parseShort(tokenizer.nextToken());

              if (arg1 < 0
                  || ((arg0 < 0 || arg0 > Definitions.ROM_SIZE)
                      && arg0 != BUILTIN_FUNCTION_ADDRESS))
                throw new ProgramException(
                    "in line " + lineNumber + ": Illegal argument - " + line);

              instructions[pc] = new VMEmulatorInstruction(opCode, arg0, arg1, indexInFunction);
              instructions[pc].setStringArg(functionName);
              break;

            case HVMInstructionSet.LABEL_CODE:
              label = currentFunction + "$" + tokenizer.nextToken();
              instructions[pc] = new VMEmulatorInstruction(opCode, (short) (-1));
              instructions[pc].setStringArg(label);
              indexInFunction--; // since Label is not a "physical" instruction
              break;

            case HVMInstructionSet.GOTO_CODE:
              label = currentFunction + "$" + tokenizer.nextToken();
              Short labelAddress = (Short) symbols.get(label);
              if (labelAddress == null)
                throw new ProgramException("in line " + lineNumber + ": Unknown label - " + label);
              arg0 = labelAddress.shortValue();

              if (arg0 < 0 || arg0 > Definitions.ROM_SIZE)
                throw new ProgramException(
                    "in line " + lineNumber + ": Illegal argument - " + line);

              instructions[pc] = new VMEmulatorInstruction(opCode, arg0, indexInFunction);
              instructions[pc].setStringArg(label);
              break;

            case HVMInstructionSet.IF_GOTO_CODE:
              label = currentFunction + "$" + tokenizer.nextToken();
              labelAddress = (Short) symbols.get(label);
              if (labelAddress == null)
                throw new ProgramException("in line " + lineNumber + ": Unknown label - " + label);

              arg0 = labelAddress.shortValue();

              if (arg0 < 0 || arg0 > Definitions.ROM_SIZE)
                throw new ProgramException(
                    "in line " + lineNumber + ": Illegal argument - " + line);

              instructions[pc] = new VMEmulatorInstruction(opCode, arg0, indexInFunction);
              instructions[pc].setStringArg(label);
              break;

              // All other instructions have either 1 or 0 arguments and require no
              // special treatment
            default:
              if (tokenizer.countTokens() == 0) {
                instructions[pc] = new VMEmulatorInstruction(opCode, indexInFunction);
              } else {
                arg0 = Short.parseShort(tokenizer.nextToken());

                if (arg0 < 0)
                  throw new ProgramException(
                      "in line " + lineNumber + ": Illegal argument - " + line);

                instructions[pc] = new VMEmulatorInstruction(opCode, arg0, indexInFunction);
              }
              break;
          }

          // check end of command
          if (tokenizer.hasMoreTokens())
            throw new ProgramException("in line " + lineNumber + ": Too many arguments - " + line);

          pc++;
          indexInFunction++;
        }

        nextPC = pc;
      }
      reader.close();
    } catch (IOException ioe) {
      throw new ProgramException("Error while reading from file");
    } catch (NumberFormatException nfe) {
      throw new ProgramException("Illegal 16-bit value");
    } catch (NoSuchElementException nsee) {
      throw new ProgramException("In line " + lineNumber + ": unexpected end of command");
    }
    if (isSlashStar) {
      throw new ProgramException("Unterminated /* comment at end of file");
    }
  }