Ejemplo n.º 1
0
 public void testWindows() throws Exception {
   // ==== Testing Windows filesystem ===
   // two options to write - WRITE, WRITE + APPEND. Simple writes just rewrite current values
   SeekableByteChannel s =
       Files.newByteChannel(
           Paths.get("test.txt"),
           StandardOpenOption.WRITE,
           StandardOpenOption.READ,
           StandardOpenOption.CREATE); // , StandardOpenOption.APPEND);
   ByteBuffer bb = ByteBuffer.wrap("12345".getBytes());
   s.truncate(0); // clear file
   s.position(0); // start position
   s.write(bb);
   bb.position(2);
   s.write(bb);
   //
   s.position(0);
   byte[] bbrb = new byte[(int) s.size()];
   s.read(ByteBuffer.wrap(bbrb));
   System.out.println(new String(bbrb)); // should be 12345345
   s.close();
 }
Ejemplo n.º 2
0
  public static void writeValue(int port, ByteBuffer data, EdisonPinManager d) {

    try {
      SeekableByteChannel channel = d.provider.newByteChannel(d.gpioValue[port], i2cOptions);
      do {
        channel.write(data);
      } while (data.hasRemaining()); // Caution, TODO: this is blocking.
      data.flip();
      channel.close();

    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 3
0
  public static void testZIP() throws Exception {
    // ==== Testing ZIP filesystem ===
    // two options to write - WRITE, WRITE + APPEND. Simple writes just rewrite current values
    URI uri = URI.create("jar:file:/test.zip!/test.txt");
    Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    FileSystem zipfs = FileSystems.newFileSystem(uri, env);
    Path pz = zipfs.provider().getPath(uri);
    SeekableByteChannel s =
        Files.newByteChannel(
            pz,
            StandardOpenOption.WRITE,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND); // , StandardOpenOption.APPEND);
    ByteBuffer bb = ByteBuffer.wrap("12345".getBytes());
    // s.truncate(0);//clear file - unsupported
    // s.position(0);//start position - unsupported
    s.write(bb);
    bb.position(2);
    s.write(bb);

    //		SeekableByteChannel s1 = Files.newByteChannel(pz, StandardOpenOption.WRITE,
    // StandardOpenOption.CREATE, StandardOpenOption.APPEND);//, StandardOpenOption.APPEND);
    //		s1.write(ByteBuffer.wrap("bb".getBytes()));
    //
    s.close();
    //		s1.close();
    s =
        Files.newByteChannel(
            pz,
            StandardOpenOption.READ,
            StandardOpenOption.CREATE); // , StandardOpenOption.APPEND);
    byte[] bbrb = new byte[(int) s.size()];
    s.read(ByteBuffer.wrap(bbrb));
    System.out.println(new String(bbrb)); // should be 12345345
    s.close();
  }
Ejemplo n.º 4
0
 private void executeWrite() throws IOException {
   System.out.println("executeWrite started.");
   try (SeekableByteChannel channel =
       Files.newByteChannel(
           Paths.get("c:/temp/channelExample.txt"),
           EnumSet.of(
               StandardOpenOption.CREATE,
               StandardOpenOption.WRITE,
               StandardOpenOption.TRUNCATE_EXISTING))) {
     String data =
         "This is a sample data ========================================"
             + "This is a sample data ========================================"
             + "This is a sample data ========================================"
             + "This is a sample data ========================================"
             + "This is a sample data ========================================\r\n";
     byte[] byteData = data.getBytes();
     ByteBuffer buf = ByteBuffer.allocate(10);
     int offset = 0;
     int length = buf.capacity();
     // read every 10 bytes and put the data into buffer and write it.
     while (true) {
       buf.clear(); // clear buffer to starting use.
       if ((offset + length) >= byteData.length) {
         length = byteData.length - offset;
       }
       buf.put(byteData, offset, length);
       buf.flip(); // you should call this method to read data from buffer.
       System.out.print(Charset.defaultCharset().decode(buf));
       buf
           .rewind(); // rewind the position because the above line used this buffer so position is
                      // at the end of data.
       channel.write(buf);
       offset += length;
       if (offset >= byteData.length) break;
     }
     System.out.println("writing data to channel finished");
   }
 }
Ejemplo n.º 5
0
  private static void processArchive(
      final Path path, final Set<Archive> output, final SettingsModelFactory settingsFactory)
      throws IOException, StarDBException {

    Archive originalArchive = new Archive(path);

    if (!originalArchive.extract()) {
      throw new IOException("Could not extract archive.");
    }

    Set<ArchiveFile> usedPaks = new HashSet<>();

    for (ArchiveFile file : originalArchive.getFiles()) {

      if (FileHelper.getExtension(file.getPath()).equals("modinfo")) {

        JsonObject o = JsonObject.readFrom(new String(file.getData()));

        String preformattedPath = o.get("path").asString().replaceAll("\"", "");

        if (preformattedPath.startsWith("./")) {
          preformattedPath = preformattedPath.replace("./", "");
        }

        if (preformattedPath.startsWith(".")) {
          preformattedPath = preformattedPath.replace(".", "");
        }

        if (preformattedPath.startsWith("/")) {
          preformattedPath = preformattedPath.replace("/", "");
        }

        Path modinfoPath = file.getPath();

        if (modinfoPath.getNameCount() > 2) {
          modinfoPath = modinfoPath.subpath(0, modinfoPath.getNameCount() - 1);
        } else if (modinfoPath.getNameCount() > 1) {
          modinfoPath = modinfoPath.getName(0);
        } else {
          modinfoPath = Paths.get("");
        }

        Archive outputArchive =
            new Archive(
                settingsFactory
                    .getInstance()
                    .getPropertyPath("modsdir")
                    .resolve(Paths.get(o.get("name").asString() + ".zip")));

        if (file.getPath().getNameCount() == 1) {
          outputArchive.addFile(new ArchiveFile(file.getData(), file.getPath(), false));
          log.debug(
              "'{}' -> '{}' relativized to '{}'", modinfoPath, file.getPath(), file.getPath());
        } else {
          outputArchive.addFile(
              new ArchiveFile(
                  file.getData(), modinfoPath.relativize(file.getPath()).normalize(), false));
          log.debug(
              "'{}' -> '{}' relativized to '{}'",
              modinfoPath,
              file.getPath(),
              modinfoPath.relativize(file.getPath()).normalize());
        }

        Path assetsPath = modinfoPath.resolve(Paths.get(preformattedPath));

        log.debug("Assets path for modinfo '{}': {}", file.getPath(), assetsPath);

        if (originalArchive.getFile(assetsPath) != null
            && !assetsPath.toString().isEmpty()
            && !originalArchive.getFile(assetsPath).isFolder()) {

          if (FileHelper.identifyType(originalArchive.getFile(assetsPath).getData())
              .equals("pak")) {

            log.debug("Assets for mod '{}' identified as .pak file: {}", modinfoPath, assetsPath);

            usedPaks.add(originalArchive.getFile(assetsPath));

            ArchiveFile modinfo = outputArchive.getFile(".modinfo");
            JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData()));
            o2.set("path", "assets");

            modinfo.setData(o2.toString().getBytes());

            // TODO Update StarDB to let me pass in a byte array instead of needing to open a file

            Path tempPath = Paths.get("tempPak" + System.nanoTime());

            SeekableByteChannel tempPakFile =
                Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
            tempPakFile.write(ByteBuffer.wrap(originalArchive.getFile(assetsPath).getData()));
            tempPakFile.close();

            AssetDatabase database = AssetDatabase.open(tempPath);

            for (String assetFile : database.getFileList()) {
              outputArchive.addFile(
                  new ArchiveFile(
                      database.getAsset(assetFile), Paths.get("assets/" + assetFile), false));
              log.trace("Asset extracted: {} | {}", assetFile, Paths.get("assets/" + assetFile));
            }

            Files.deleteIfExists(tempPath);
          }

        } else {

          ArchiveFile modinfo = outputArchive.getFile(".modinfo");
          JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData()));
          o2.set("path", "assets");

          modinfo.setData(o2.toString().getBytes());

          log.debug("Assets for mod '{}' is a standard assets folder: {}", modinfoPath, assetsPath);

          for (ArchiveFile f2 : originalArchive.getFiles()) {

            if ((assetsPath.toString().isEmpty() || f2.getPath().startsWith(assetsPath))
                && !f2.isFolder()
                && !f2.getPath().toString().endsWith(".modinfo")) {

              if (modinfoPath.toString().isEmpty()) {

                if (f2.getPath().getNameCount() == 1) {

                  if (!f2.getPath().startsWith(assetsPath)) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/").resolve(f2.getPath()).normalize(),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()).normalize());
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }

                } else {

                  if (!f2.getPath().startsWith(assetsPath)) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/").resolve(f2.getPath()).normalize(),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()).normalize());
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }
                }

              } else {

                if (f2.getPath().getNameCount() == 1) {

                  if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(), Paths.get("assets/").resolve(f2.getPath()), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()));
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }

                } else {

                  if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/")
                                .resolve(modinfoPath.relativize(f2.getPath()).normalize()),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/")
                            .resolve(modinfoPath.relativize(f2.getPath()).normalize()));
                  } else {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(), modinfoPath.relativize(f2.getPath()).normalize(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        modinfoPath.relativize(f2.getPath()).normalize());
                  }
                }
              }
            }
          }
        }

        outputArchive.writeToFile(
            settingsFactory
                .getInstance()
                .getPropertyPath("modsdir")
                .resolve(Paths.get(o.get("name").asString() + ".zip"))
                .toFile()); // TODO

        output.add(outputArchive);
      }
    }

    for (ArchiveFile file : originalArchive.getFiles()) {

      if (!usedPaks.contains(file)
          && !file.isFolder()
          && FileHelper.identifyType(file.getData()).equals("pak")) {

        log.debug("Additional .pak identified: {}", file.getPath());

        Path tempPath = Paths.get("tempPak" + System.nanoTime());

        SeekableByteChannel tempPakFile =
            Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        tempPakFile.write(ByteBuffer.wrap(file.getData()));
        tempPakFile.close();

        AssetDatabase database = AssetDatabase.open(tempPath);
        log.debug(database.getFileList());

        if (database.getAsset("/pak.modinfo") != null) {
          log.debug("{} has a .modinfo file, parsing into mod.", file.getPath());
          processPakFile(tempPath, output, settingsFactory);
        } else {
          log.debug("{} does not contain a .modinfo file, skipping.", file.getPath());
        }

        Files.deleteIfExists(tempPath);
      }
    }
  }