Exemplo n.º 1
0
  /**
   * Reads a base class overrride from a resource file
   *
   * @param binaryClassName
   * @param fileName
   */
  private void registerBaseClassOverride(String binaryClassName, String fileName) {
    try {
      Method mDefineClass =
          ClassLoader.class.getDeclaredMethod(
              "defineClass", String.class, byte[].class, int.class, int.class);
      mDefineClass.setAccessible(true);

      InputStream resourceInputStream =
          LiteLoader.class.getResourceAsStream("/classes/" + fileName + ".bin");

      if (resourceInputStream != null) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        for (int readBytes = resourceInputStream.read();
            readBytes >= 0;
            readBytes = resourceInputStream.read()) {
          outputStream.write(readBytes);
        }

        byte[] data = outputStream.toByteArray();

        outputStream.close();
        resourceInputStream.close();

        logger.info("Defining class override for " + binaryClassName);
        mDefineClass.invoke(
            Minecraft.class.getClassLoader(), binaryClassName, data, 0, data.length);
      } else {
        logger.info("Error defining class override for " + binaryClassName + ", file not found");
      }
    } catch (Throwable th) {
      logger.log(Level.WARNING, "Error defining class override for " + binaryClassName, th);
    }
  }
Exemplo n.º 2
0
  public void close() {
    InputStream is = _is;
    _is = null;

    InputStream fileIs = _fileIs;
    _fileIs = null;

    if (is != null) {
      try {
        is.close();
      } catch (Exception e) {
        throw new IllegalStateException(e);
      }
    }

    if (fileIs != null) {
      try {
        fileIs.close();
      } catch (Exception e) {
        throw new IllegalStateException(e);
      }
    }
  }
Exemplo n.º 3
0
  private void loadConfiguration() throws RuntimeConfigException {
    trace(String.format("Loading configuration from [%s]", configFilePath));
    try {
      InputStream stream = new FileInputStream(configFilePath);
      // InputStream stream1 = new FileInputStream("config/ocsswws.config");
      try {
        Properties fileProperties = new Properties();
        fileProperties.load(stream);
        // @todo check tests - code was not backward compatible with Java 5
        // so i changed it - but this is not the only place of uncompatibilty
        // add default properties so that they override file properties
        // Set<String> propertyNames = fileProperties.stringPropertyNames();
        //                for (String propertyName : propertyNames) {
        //                    String propertyValue = fileProperties.getProperty(propertyName);
        //                    if (!isPropertySet(propertyName)) {
        //                        setProperty(propertyName, propertyValue);
        //                        trace(String.format("Configuration property [%s] added",
        // propertyName));
        //                    } else {
        //                        trace(String.format("Configuration property [%s] ignored",
        // propertyName));
        //                    }
        //                }

        Enumeration<?> enumeration = fileProperties.propertyNames();
        while (enumeration.hasMoreElements()) {
          final Object key = enumeration.nextElement();
          if (key instanceof String) {
            final Object value = fileProperties.get(key);
            if (value instanceof String) {
              final String keyString = (String) key;
              String propertyValue = fileProperties.getProperty(keyString);
              if (!isPropertySet(keyString)) {
                setProperty(keyString, propertyValue);
                trace(String.format("Configuration property [%s] added", keyString));
              } else {
                trace(String.format("Configuration property [%s] ignored", keyString));
              }
            }
          }
        }
      } finally {
        stream.close();
      }
    } catch (IOException e) {
      throw new RuntimeConfigException(
          String.format("Failed to load configuration [%s]", configFilePath), e);
    }
  }
Exemplo n.º 4
0
 public static int pipe_stream(InputStream is, OutputStream os, boolean wantsKeepOpen)
     throws IOException { // U: copia de un stream al otro
   int cnt = 0;
   int n;
   byte[] buffer = new byte[BUFF_SZ];
   while ((n = is.read(buffer)) > -1) {
     cnt += n;
     os.write(buffer, 0, n);
   }
   if (!wantsKeepOpen) {
     is.close();
     os.close();
   }
   return cnt;
 }
    @Override
    public void run() {
      byte[] buf = new byte[100];
      try {
        int len;
        while ((len = in.read(buf)) > 0) {
          String output = new String(buf, 0, len);
          Thread t = Thread.currentThread();
          System.out.println(
              "thread " + t.getName() + " " + t.getId() + ", read " + len + " bytes: " + output);
        }

      } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to read", e);

      } finally {
        try {
          in.close();
        } catch (IOException e) {
          logger.log(Level.SEVERE, "Failed to close", e);
        }
      }
    }
Exemplo n.º 6
0
 public static TypeList load(String gamePath, String dataName) {
   System.out.println("load:" + dataName);
   InputStream is = null;
   Savable sav = null;
   try {
     File file =
         new File(
             System.getProperty("user.dir")
                 + File.separator
                 + gamePath
                 + File.separator
                 + dataName);
     if (!file.exists()) {
       return null;
     }
     is = new BufferedInputStream(new FileInputStream(file));
     // is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
     XMLImporter imp = XMLImporter.getInstance();
     // if (manager != null) {
     //     imp.setAssetManager(manager);
     // }
     sav = imp.load(is);
   } catch (IOException ex) {
     Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
     ex.printStackTrace();
   } finally {
     if (is != null) {
       try {
         is.close();
       } catch (IOException ex) {
         Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
         ex.printStackTrace();
       }
     }
   }
   return (TypeList) sav;
 }