Exemplo n.º 1
0
  @Test
  public void testCoinbaseHeightTestnet() throws Exception {
    // Testnet block 21066 (hash 0000000004053156021d8e42459d284220a7f6e087bf78f30179c3703ca4eefa)
    // contains a coinbase transaction whose height is two bytes, which is
    // shorter than we see in most other cases.

    Block block =
        TestNet3Params.get()
            .getDefaultSerializer()
            .makeBlock(
                ByteStreams.toByteArray(getClass().getResourceAsStream("block_testnet21066.dat")));

    // Check block.
    assertEquals(
        "0000000004053156021d8e42459d284220a7f6e087bf78f30179c3703ca4eefa",
        block.getHashAsString());
    block.verify(21066, EnumSet.of(Block.VerifyFlag.HEIGHT_IN_COINBASE));

    // Testnet block 32768 (hash 000000007590ba495b58338a5806c2b6f10af921a70dbd814e0da3c6957c0c03)
    // contains a coinbase transaction whose height is three bytes, but could
    // fit in two bytes. This test primarily ensures script encoding checks
    // are applied correctly.

    block =
        TestNet3Params.get()
            .getDefaultSerializer()
            .makeBlock(
                ByteStreams.toByteArray(getClass().getResourceAsStream("block_testnet32768.dat")));

    // Check block.
    assertEquals(
        "000000007590ba495b58338a5806c2b6f10af921a70dbd814e0da3c6957c0c03",
        block.getHashAsString());
    block.verify(32768, EnumSet.of(Block.VerifyFlag.HEIGHT_IN_COINBASE));
  }
  @Test
  public void testStore() throws Exception {
    InputStream data = ByteStreams.newInputStreamSupplier("hejsan vad bra".getBytes()).getInput();

    String path =
        Joiner.on(File.separatorChar)
            .join("com", "bygg", "bygg-test-artifact", "2.3", "bygg-test-artifact-2.3.jar");

    File expectedFile = new File(repositoryDirectory, path);

    expectedFile.delete();
    assertFalse(expectedFile.exists());

    URL actualURl = repository.store(new ArtifactVersion(ARTIFACT, "2.3"), data);

    assertThat(
        actualURl,
        equalTo(
            new URL(
                "file:"
                    + repositoryDirectory.getAbsolutePath()
                    + "/com/bygg/bygg-test-artifact/2.3/bygg-test-artifact-2.3.jar")));

    assertTrue(expectedFile.exists());

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    ByteStreams.copy(new FileInputStream(expectedFile), outputStream);

    assertThat(outputStream.toString(), equalTo("hejsan vad bra"));
  }
Exemplo n.º 3
0
  /**
   * Checks that the contents of this byte source are equal to the contents of the given byte
   * source.
   *
   * @throws IOException if an I/O error occurs in the process of reading from this source or {@code
   *     other}
   */
  public boolean contentEquals(ByteSource other) throws IOException {
    checkNotNull(other);

    byte[] buf1 = new byte[BUF_SIZE];
    byte[] buf2 = new byte[BUF_SIZE];

    Closer closer = Closer.create();
    try {
      InputStream in1 = closer.register(openStream());
      InputStream in2 = closer.register(other.openStream());
      while (true) {
        int read1 = ByteStreams.read(in1, buf1, 0, BUF_SIZE);
        int read2 = ByteStreams.read(in2, buf2, 0, BUF_SIZE);
        if (read1 != read2 || !Arrays.equals(buf1, buf2)) {
          return false;
        } else if (read1 != BUF_SIZE) {
          return true;
        }
      }
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }
Exemplo n.º 4
0
 @Test
 public void testRc4WithEmptyData() throws IOException {
   Closer closer = Closer.create();
   try {
     // generate random input data
     byte[] randomData = TestUtils.randomBytes(0, true);
     InputStream randomInputStream = new ByteArrayInputStream(randomData);
     closer.register(randomInputStream);
     // generate cipher key
     byte[] keyBytes = Rc4Utils.generateKey();
     // encrypt and write
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     closer.register(byteArrayOutputStream);
     OutputStream encryptedOutputStream = Rc4Utils.encrypt(byteArrayOutputStream, keyBytes);
     closer.register(encryptedOutputStream);
     ByteStreams.copy(randomInputStream, encryptedOutputStream);
     byte[] encryptedBytes = byteArrayOutputStream.toByteArray();
     // read and decrypt
     InputStream encryptedInputStream = new ByteArrayInputStream(encryptedBytes);
     closer.register(encryptedInputStream);
     InputStream decryptedInputStream = Rc4Utils.decrypt(encryptedInputStream, keyBytes);
     closer.register(decryptedInputStream);
     byte[] decryptedData = ByteStreams.toByteArray(decryptedInputStream);
     // checking data
     Assert.assertArrayEquals(randomData, decryptedData);
     Assert.assertArrayEquals(Rc4Utils.encrypt(randomData, keyBytes), encryptedBytes);
     Assert.assertArrayEquals(Rc4Utils.decrypt(encryptedBytes, keyBytes), randomData);
   } catch (Throwable t) {
     closer.rethrow(t);
   } finally {
     closer.close();
   }
 }
 public void testMemberSeek() throws IOException {
   GZIPMembersInputStream gzin = new GZIPMembersInputStream(new ByteArrayInputStream(allfour_gz));
   gzin.setEofEachMember(true);
   gzin.compressedSeek(noise1k_gz.length + noise32k_gz.length);
   int count2 = IOUtils.copy(gzin, ByteStreams.nullOutputStream());
   assertEquals("wrong 1-byte member count", 1, count2);
   //        assertEquals("wrong Member number", 2, gzin.getMemberNumber());
   assertEquals(
       "wrong Member2 start",
       noise1k_gz.length + noise32k_gz.length,
       gzin.getCurrentMemberStart());
   assertEquals(
       "wrong Member2 end",
       noise1k_gz.length + noise32k_gz.length + a_gz.length,
       gzin.getCurrentMemberEnd());
   gzin.nextMember();
   int count3 = IOUtils.copy(gzin, ByteStreams.nullOutputStream());
   assertEquals("wrong 5-byte member count", 5, count3);
   //        assertEquals("wrong Member number", 3, gzin.getMemberNumber());
   assertEquals(
       "wrong Member3 start",
       noise1k_gz.length + noise32k_gz.length + a_gz.length,
       gzin.getCurrentMemberStart());
   assertEquals(
       "wrong Member3 end",
       noise1k_gz.length + noise32k_gz.length + a_gz.length + hello_gz.length,
       gzin.getCurrentMemberEnd());
   gzin.nextMember();
   int countEnd = IOUtils.copy(gzin, ByteStreams.nullOutputStream());
   assertEquals("wrong eof count", 0, countEnd);
 }
Exemplo n.º 6
0
  public static void main(String[] args) throws IOException {

    InputStream inputStream = null;
    OutputStream outputStream = null;
    ByteStreams.copy(inputStream, outputStream);
    ByteStreams.newDataOutput();
  }
Exemplo n.º 7
0
  @Test
  public void isBIPs() throws Exception {
    final MainNetParams mainnet = MainNetParams.get();
    final Block genesis = mainnet.getGenesisBlock();
    assertFalse(genesis.isBIP34());
    assertFalse(genesis.isBIP66());
    assertFalse(genesis.isBIP65());
    assertFalse(genesis.isBIP101());

    // 227835/00000000000001aa077d7aa84c532a4d69bdbff519609d1da0835261b7a74eb6: last version 1 block
    final Block block227835 =
        mainnet
            .getDefaultSerializer()
            .makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block227835.dat")));
    assertFalse(block227835.isBIP34());
    assertFalse(block227835.isBIP66());
    assertFalse(block227835.isBIP65());
    assertFalse(block227835.isBIP101());

    // 227836/00000000000000d0dfd4c9d588d325dce4f32c1b31b7c0064cba7025a9b9adcc: version 2 block
    final Block block227836 =
        mainnet
            .getDefaultSerializer()
            .makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block227836.dat")));
    assertTrue(block227836.isBIP34());
    assertFalse(block227836.isBIP66());
    assertFalse(block227836.isBIP65());
    assertFalse(block227836.isBIP101());

    // 363703/0000000000000000011b2a4cb91b63886ffe0d2263fd17ac5a9b902a219e0a14: version 3 block
    final Block block363703 =
        mainnet
            .getDefaultSerializer()
            .makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block363703.dat")));
    assertTrue(block363703.isBIP34());
    assertTrue(block363703.isBIP66());
    assertFalse(block363703.isBIP65());
    assertFalse(block363703.isBIP101());

    // 383616/00000000000000000aab6a2b34e979b09ca185584bd1aecf204f24d150ff55e9: version 4 block
    final Block block383616 =
        mainnet
            .getDefaultSerializer()
            .makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block383616.dat")));
    assertTrue(block383616.isBIP34());
    assertTrue(block383616.isBIP66());
    assertTrue(block383616.isBIP65());
    assertFalse(block383616.isBIP101());

    // 370661/00000000000000001416a613602d73bbe5c79170fd8f39d509896b829cf9021e
    final Block block370661 =
        mainnet
            .getDefaultSerializer()
            .makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block370661.dat")));
    assertTrue(block370661.isBIP34());
    assertTrue(block370661.isBIP66());
    assertTrue(block370661.isBIP65());
    assertTrue(block370661.isBIP101());
  }
Exemplo n.º 8
0
 protected Payload doSlice(InputStream content, long offset, long length) {
   try {
     ByteStreams.skipFully(content, offset);
   } catch (IOException ioe) {
     throw Throwables.propagate(ioe);
   }
   return new InputStreamPayload(ByteStreams.limit(content, length));
 }
Exemplo n.º 9
0
 protected Statement peekNextStatement() throws Exception {
   if (!statefulHasNext()) return null;
   Map.Entry<Key, Value> entry = iter.peek();
   Key key = entry.getKey();
   if (DOC.equals(key.getColumnFamily()))
     return readStatement(
         ByteStreams.newDataInput(key.getColumnQualifier().getBytes()), VALUE_FACTORY);
   else
     return readStatement(
         ByteStreams.newDataInput(key.getColumnQualifier().getBytes()), VALUE_FACTORY, false);
 }
  @Override
  public void getSegmentFiles(DataSegment segment, File outDir) throws SegmentLoadingException {
    S3Coords s3Coords = new S3Coords(segment);

    log.info("Pulling index at path[%s] to outDir[%s]", s3Coords, outDir);

    if (!isObjectInBucket(s3Coords)) {
      throw new SegmentLoadingException("IndexFile[%s] does not exist.", s3Coords);
    }

    if (!outDir.exists()) {
      outDir.mkdirs();
    }

    if (!outDir.isDirectory()) {
      throw new ISE("outDir[%s] must be a directory.", outDir);
    }

    long startTime = System.currentTimeMillis();
    S3Object s3Obj = null;

    try {
      s3Obj = s3Client.getObject(new S3Bucket(s3Coords.bucket), s3Coords.path);

      InputStream in = null;
      try {
        in = s3Obj.getDataInputStream();
        final String key = s3Obj.getKey();
        if (key.endsWith(".zip")) {
          CompressionUtils.unzip(in, outDir);
        } else if (key.endsWith(".gz")) {
          final File outFile = new File(outDir, toFilename(key, ".gz"));
          ByteStreams.copy(new GZIPInputStream(in), Files.newOutputStreamSupplier(outFile));
        } else {
          ByteStreams.copy(
              in, Files.newOutputStreamSupplier(new File(outDir, toFilename(key, ""))));
        }
        log.info(
            "Pull of file[%s] completed in %,d millis",
            s3Obj, System.currentTimeMillis() - startTime);
      } catch (IOException e) {
        FileUtils.deleteDirectory(outDir);
        throw new SegmentLoadingException(e, "Problem decompressing object[%s]", s3Obj);
      } finally {
        Closeables.closeQuietly(in);
      }
    } catch (Exception e) {
      throw new SegmentLoadingException(e, e.getMessage());
    } finally {
      S3Utils.closeStreamsQuietly(s3Obj);
    }
  }
  @SuppressWarnings("deprecation")
  public void testMemberIterator() throws IOException {
    GZIPMembersInputStream gzin = new GZIPMembersInputStream(new ByteArrayInputStream(allfour_gz));
    Iterator<GZIPMembersInputStream> iter = gzin.memberIterator();
    assertTrue(iter.hasNext());
    GZIPMembersInputStream gzMember0 = iter.next();
    int count0 = IOUtils.copy(gzMember0, ByteStreams.nullOutputStream());
    assertEquals("wrong 1k member count", 1024, count0);
    assertEquals("wrong member number", 0, gzin.getMemberNumber());
    assertEquals("wrong member0 start", 0, gzin.getCurrentMemberStart());
    assertEquals("wrong member0 end", noise1k_gz.length, gzin.getCurrentMemberEnd());

    assertTrue(iter.hasNext());
    GZIPMembersInputStream gzMember1 = iter.next();
    int count1 = IOUtils.copy(gzMember1, ByteStreams.nullOutputStream());
    assertEquals("wrong 32k member count", (32 * 1024), count1);
    assertEquals("wrong member number", 1, gzin.getMemberNumber());
    assertEquals("wrong member1 start", noise1k_gz.length, gzin.getCurrentMemberStart());
    assertEquals(
        "wrong member1 end", noise1k_gz.length + noise32k_gz.length, gzin.getCurrentMemberEnd());

    assertTrue(iter.hasNext());
    GZIPMembersInputStream gzMember2 = iter.next();
    int count2 = IOUtils.copy(gzMember2, ByteStreams.nullOutputStream());
    assertEquals("wrong 1-byte member count", 1, count2);
    assertEquals("wrong member number", 2, gzin.getMemberNumber());
    assertEquals(
        "wrong member2 start",
        noise1k_gz.length + noise32k_gz.length,
        gzin.getCurrentMemberStart());
    assertEquals(
        "wrong member2 end",
        noise1k_gz.length + noise32k_gz.length + a_gz.length,
        gzin.getCurrentMemberEnd());

    assertTrue(iter.hasNext());
    GZIPMembersInputStream gzMember3 = iter.next();
    int count3 = IOUtils.copy(gzMember3, ByteStreams.nullOutputStream());
    assertEquals("wrong 5-byte member count", 5, count3);
    assertEquals("wrong member number", 3, gzin.getMemberNumber());
    assertEquals(
        "wrong member3 start",
        noise1k_gz.length + noise32k_gz.length + a_gz.length,
        gzin.getCurrentMemberStart());
    assertEquals(
        "wrong member3 end",
        noise1k_gz.length + noise32k_gz.length + a_gz.length + hello_gz.length,
        gzin.getCurrentMemberEnd());

    assertFalse(iter.hasNext());
  }
 public void testReadPerMemberSixSmall() throws IOException {
   GZIPMembersInputStream gzin = new GZIPMembersInputStream(new ByteArrayInputStream(sixsmall_gz));
   gzin.setEofEachMember(true);
   for (int i = 0; i < 3; i++) {
     int count2 = IOUtils.copy(gzin, ByteStreams.nullOutputStream());
     assertEquals("wrong 1-byte member count", 1, count2);
     gzin.nextMember();
     int count3 = IOUtils.copy(gzin, ByteStreams.nullOutputStream());
     assertEquals("wrong 5-byte member count", 5, count3);
     gzin.nextMember();
   }
   int countEnd = IOUtils.copy(gzin, ByteStreams.nullOutputStream());
   assertEquals("wrong eof count", 0, countEnd);
 }
Exemplo n.º 13
0
 /**
  * Gets content of file as array of bytes.
  *
  * @return content of file as stream
  * @throws IOException if an i/o error occurs
  * @throws ServerException if other error occurs
  */
 public byte[] contentAsBytes() throws IOException, ServerException {
   final ContentStream contentStream = getContentStream();
   final int contentLength = (int) contentStream.getLength();
   if (contentLength == 0) {
     return new byte[0];
   }
   try (InputStream stream = contentStream.getStream()) {
     if (contentLength < 0) {
       return ByteStreams.toByteArray(stream);
     }
     final byte[] b = new byte[contentLength];
     ByteStreams.readFully(stream, b);
     return b;
   }
 }
Exemplo n.º 14
0
  /**
   * Similar to {@link #createZip(Collection, Path)}, but also takes a list of additional files to
   * write in the zip, including their contents, as a map. It's assumed only paths that should not
   * be ignored are passed to this method.
   */
  public void createZip(
      Collection<Path> pathsToIncludeInZip,
      Path out,
      ImmutableMap<Path, String> additionalFileContents)
      throws IOException {
    try (CustomZipOutputStream zip = ZipOutputStreams.newOutputStream(out)) {
      for (Path path : pathsToIncludeInZip) {
        boolean isDirectory = isDirectory(path);

        String entryName = path.toString();
        if (isDirectory) {
          entryName += "/";
        }
        CustomZipEntry entry = new CustomZipEntry(entryName);

        // We want deterministic ZIPs, so avoid mtimes.
        entry.setTime(0);

        // Support executable files.  If we detect this file is executable, store this
        // information as 0100 in the field typically used in zip implementations for
        // POSIX file permissions.  We'll use this information when unzipping.
        if (isExecutable(path)) {
          entry.setExternalAttributes(
              MorePosixFilePermissions.toMode(EnumSet.of(PosixFilePermission.OWNER_EXECUTE)) << 16);
        }

        zip.putNextEntry(entry);
        if (!isDirectory) {
          try (InputStream input = newFileInputStream(path)) {
            ByteStreams.copy(input, zip);
          }
        }
        zip.closeEntry();
      }

      for (Map.Entry<Path, String> fileContentsEntry : additionalFileContents.entrySet()) {
        CustomZipEntry entry = new CustomZipEntry(fileContentsEntry.getKey().toString());
        // We want deterministic ZIPs, so avoid mtimes.
        entry.setTime(0);
        zip.putNextEntry(entry);
        try (InputStream stream =
            new ByteArrayInputStream(fileContentsEntry.getValue().getBytes(Charsets.UTF_8))) {
          ByteStreams.copy(stream, zip);
        }
        zip.closeEntry();
      }
    }
  }
Exemplo n.º 15
0
 private static String outputAsString(Process process) {
   try {
     return new String(ByteStreams.toByteArray(process.getInputStream()));
   } catch (IOException exception) {
     throw new AdbException("Could not read output of ADB command.", exception);
   }
 }
  private void extractLauncher(URL sourceLauncher, File targetLauncher) {
    checkNotNull(sourceLauncher);
    checkNotNull(targetLauncher);

    InputStream is = null;
    OutputStream os = null;

    try {
      targetLauncher.getParentFile().mkdirs();

      if (!targetLauncher.exists()) {
        Files.touch(targetLauncher);
      }

      is = sourceLauncher.openStream();
      os = new FileOutputStream(targetLauncher);

      ByteStreams.copy(is, os);
    } catch (IOException e) {
      throw new OperaRunnerException("Cannot write file to disk: " + e.getMessage());
    } finally {
      Closeables.closeQuietly(is);
      Closeables.closeQuietly(os);
    }

    logger.fine("New launcher copied to " + targetLauncher.getPath());
  }
Exemplo n.º 17
0
  // adds file to S3 Data store or offline cache (if working offline)
  public String addPointSet(File pointSetFile, String pointSetId) throws IOException {
    if (pointSetId == null) throw new NullPointerException("null point set id");

    File renamedPointSetFile = new File(POINT_DIR, pointSetId + ".json");

    if (renamedPointSetFile.exists()) return pointSetId;

    FileUtils.copyFile(pointSetFile, renamedPointSetFile);

    if (!this.workOffline) {
      // only upload if it doesn't exist
      try {
        s3.getObjectMetadata(pointsetBucket, pointSetId + ".json.gz");
      } catch (AmazonServiceException e) {
        // gzip compression in storage, not because we're worried about file size but to speed file
        // transfer
        FileInputStream fis = new FileInputStream(pointSetFile);
        File tempFile = File.createTempFile(pointSetId, ".json.gz");
        FileOutputStream fos = new FileOutputStream(tempFile);
        GZIPOutputStream gos = new GZIPOutputStream(fos);

        try {
          ByteStreams.copy(fis, gos);
        } finally {
          gos.close();
          fis.close();
        }

        s3.putObject(pointsetBucket, pointSetId + ".json.gz", tempFile);
        tempFile.delete();
      }
    }

    return pointSetId;
  }
Exemplo n.º 18
0
    @Override
    public PointSet load(String pointSetId) throws Exception {

      File cachedFile;

      if (!workOffline) {
        // get pointset metadata from S3
        cachedFile = new File(POINT_DIR, pointSetId + ".json");
        if (!cachedFile.exists()) {
          POINT_DIR.mkdirs();

          S3Object obj = s3.getObject(pointsetBucket, pointSetId + ".json.gz");
          ObjectMetadata objMet = obj.getObjectMetadata();
          FileOutputStream fos = new FileOutputStream(cachedFile);
          GZIPInputStream gis = new GZIPInputStream(obj.getObjectContent());
          try {
            ByteStreams.copy(gis, fos);
          } finally {
            fos.close();
            gis.close();
          }
        }
      } else cachedFile = new File(POINT_DIR, pointSetId + ".json");

      // grab it from the cache

      return PointSet.fromGeoJson(cachedFile);
    }
Exemplo n.º 19
0
 @Test
 public void checkLevelDat() throws IOException {
   InputStream stream = getClass().getResourceAsStream("level.dat");
   byte[] data = ByteStreams.toByteArray(stream);
   assertEquals("a7ada83978ce522c17e8e21f405759b9eda1399540858fdbae5bc03e2717e2fa", digest(data));
   stream.close();
 }
Exemplo n.º 20
0
 /** Blocking operation that copies the processes stdout/stderr to this JVM's stdout/stderr. */
 private static void redirectIO(final Process process) throws IOException {
   // Because chmod doesn't have a lot of error or output messages, its safe to process the output
   // after the process is done. As of java 7, you can have the process redirect to System.out
   // and System.err without forking a process.
   // TODO when java 6 support is dropped, switch to
   // http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#inheritIO()
   Closer closer = Closer.create();
   try {
     ByteStreams.copy(closer.register(process.getInputStream()), System.out);
     ByteStreams.copy(closer.register(process.getErrorStream()), System.err);
   } catch (Throwable e) {
     throw closer.rethrow(e);
   } finally {
     closer.close();
   }
 }
Exemplo n.º 21
0
  @Override
  public void putEntry(FileLike fileLike) throws IOException {
    String name = fileLike.getRelativePath();
    // Tracks unique entry names and avoids duplicates.  This is, believe it or not, how
    // proguard seems to handle merging multiple -injars into a single -outjar.
    if (!containsEntry(fileLike)) {
      entryNames.add(name);
      outStream.putNextEntry(new ZipEntry(name));
      try (InputStream in = fileLike.getInput()) {
        ByteStreams.copy(in, outStream);
      }

      // Make sure FileLike#getSize didn't lie (or we forgot to call canPutEntry).
      DalvikStatsTool.Stats stats = dalvikStatsCache.getStats(fileLike);
      Preconditions.checkState(
          !isEntryTooBig(fileLike),
          "Putting entry %s (%s) exceeded maximum size of %s",
          name,
          stats.estimatedLinearAllocSize,
          linearAllocLimit);
      currentLinearAllocSize += stats.estimatedLinearAllocSize;
      currentMethodReferences.addAll(stats.methodReferences);
      String report =
          String.format(
              "%d %d %s\n", stats.estimatedLinearAllocSize, stats.methodReferences.size(), name);
      Files.append(report, reportFile, Charsets.UTF_8);
    }
  }
Exemplo n.º 22
0
 /**
  * ‘file.append’ attempts to append the files named by its second argument to those named by its
  * first. The R subscript recycling rule is used to align names given in vectors of different
  * lengths.
  */
 @Internal("file.append")
 @DataParallel
 public static boolean fileAppend(
     @Current Context context, String destFileName, String sourceFileName) {
   try {
     FileObject sourceFile = context.resolveFile(sourceFileName);
     if (!sourceFile.exists()) {
       return false;
     }
     FileObject destFile = context.resolveFile(destFileName);
     OutputStream out = destFile.getContent().getOutputStream(true);
     try {
       InputStream in = sourceFile.getContent().getInputStream();
       try {
         ByteStreams.copy(in, out);
       } finally {
         try {
           in.close();
         } catch (Exception ignored) {
         }
       }
     } finally {
       try {
         out.close();
       } catch (Exception ignored) {
       }
     }
     return true;
   } catch (Exception e) {
     return false;
   }
 }
  /** Test of getBlob method, of class FilesystemAsyncBlobStore. */
  public void testGetBlob() throws IOException {
    String blobKey = TestUtils.createRandomBlobKey();
    GetOptions options = null;
    Blob resultBlob;

    blobStore.createContainerInLocation(null, CONTAINER_NAME);

    resultBlob = blobStore.getBlob(CONTAINER_NAME, blobKey, options);
    assertNull(resultBlob, "Blob exists");

    // create blob
    TestUtils.createBlobsInContainer(CONTAINER_NAME, blobKey);

    resultBlob = blobStore.getBlob(CONTAINER_NAME, blobKey, options);

    assertNotNull(resultBlob, "Blob exists");
    // checks file content
    InputSupplier<FileInputStream> expectedFile =
        Files.newInputStreamSupplier(new File(TARGET_CONTAINER_NAME, blobKey));
    assertTrue(
        ByteStreams.equal(expectedFile, resultBlob.getPayload()),
        "Blob payload differs from file content");
    // metadata are verified in the test for blobMetadata, so no need to
    // perform a complete test here
    assertNotNull(resultBlob.getMetadata(), "Metadata null");
    MutableBlobMetadata metadata = resultBlob.getMetadata();
    assertEquals(blobKey, metadata.getName(), "Wrong blob metadata");
  }
Exemplo n.º 24
0
 public static TeredoInfo getTeredoInfo(Inet6Address paramInet6Address) {
   Preconditions.checkArgument(
       isTeredoAddress(paramInet6Address),
       "Address '%s' is not a Teredo address.",
       new Object[] {toAddrString(paramInet6Address)});
   byte[] arrayOfByte1 = paramInet6Address.getAddress();
   Inet4Address localInet4Address1 = getInet4Address(Arrays.copyOfRange(arrayOfByte1, 4, 8));
   int i = ByteStreams.newDataInput(arrayOfByte1, 8).readShort() & 0xFFFF;
   int j = (ByteStreams.newDataInput(arrayOfByte1, 10).readShort() ^ 0xFFFFFFFF) & 0xFFFF;
   byte[] arrayOfByte2 = Arrays.copyOfRange(arrayOfByte1, 12, 16);
   for (int k = 0; k < arrayOfByte2.length; k++) {
     arrayOfByte2[k] = ((byte) (arrayOfByte2[k] ^ 0xFFFFFFFF));
   }
   Inet4Address localInet4Address2 = getInet4Address(arrayOfByte2);
   return new TeredoInfo(localInet4Address1, localInet4Address2, j, i);
 }
  private StoredObject createCombinedObjectSmall(CombinedStoredObject combinedObject) {
    ImmutableList.Builder<InputSupplier<InputStream>> builder = ImmutableList.builder();
    List<URI> sourceParts =
        Lists.transform(combinedObject.getSourceParts(), StoredObject.GET_LOCATION_FUNCTION);
    for (URI sourcePart : sourceParts) {
      builder.add(getInputSupplier(sourcePart));
    }
    InputSupplier<InputStream> source = ByteStreams.join(builder.build());

    File tempFile = null;
    try {
      tempFile =
          File.createTempFile(
              S3StorageHelper.getS3FileName(combinedObject.getLocation()), ".small.s3.data");
      Files.copy(source, tempFile);
      StoredObject result = putObject(combinedObject.getLocation(), tempFile);
      return result;
    } catch (IOException e) {
      throw Throwables.propagate(e);
    } finally {
      if (tempFile != null) {
        tempFile.delete();
      }
    }
  }
Exemplo n.º 26
0
  public void doStreamed(
      final EntityManager em, final ArtifactEntity ae, final ThrowingConsumer<Path> fileConsumer)
      throws IOException {
    final Path tmp = StreamServiceHelper.createTempFile(ae.getName());

    try {
      try (OutputStream os = new BufferedOutputStream(new FileOutputStream(tmp.toFile()))) {
        streamArtifact(
            em,
            ae,
            (ai, in) -> {
              ByteStreams.copy(in, os);
            });
      }

      try {
        fileConsumer.accept(tmp);
      } catch (final IOException e) {
        throw e;
      } catch (final Exception e) {
        throw new RuntimeException(e);
      }
    } finally {
      Files.deleteIfExists(tmp);
    }
  }
 @Override
 public void handle(NetHandler handler, Packet131MapData mapData) {
   ByteArrayDataInput in = ByteStreams.newDataInput(mapData.itemData);
   EntityPlayer player = handler.getPlayer();
   World world = player.worldObj;
   int x;
   int y;
   int z;
   switch (mapData.uniqueID) { // this is your PACKET_ID from above
     case EXPORTER_GUI:
       x = in.readInt();
       y = in.readInt();
       z = in.readInt();
       if (QBEBlockType.NonSolids.EXPORTER.matches(world, x, y, z)) {
         TileEntityExporter eb = (TileEntityExporter) world.getBlockTileEntity(x, y, z);
         eb.xSize = in.readInt();
         eb.ySize = in.readInt();
         eb.zSize = in.readInt();
       }
       break;
     case INTERFACE_GUI_SAVE:
       x = in.readInt();
       y = in.readInt();
       z = in.readInt();
       int dimX = in.readInt();
       int dimY = in.readInt();
       int dimZ = in.readInt();
   }
 }
Exemplo n.º 28
0
 @Test
 public void checkTestNbt() throws IOException {
   InputStream stream = getClass().getResourceAsStream("test.nbt");
   byte[] data = ByteStreams.toByteArray(stream);
   assertEquals("8ce8cd785334bf9736d7e69b00c69238eff88ab6bd471c8e0a743e382223606d", digest(data));
   stream.close();
 }
Exemplo n.º 29
0
 private void onRequestUpload() {
   if (tempAttachments.size() < 3) {
     FileChooser fileChooser = new FileChooser();
     fileChooser.setTitle("Open file to attach");
     /* if (Utilities.isUnix())
     fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));*/
     File result = fileChooser.showOpenDialog(stage);
     if (result != null) {
       try {
         URL url = result.toURI().toURL();
         try (InputStream inputStream = url.openStream()) {
           byte[] filesAsBytes = ByteStreams.toByteArray(inputStream);
           if (filesAsBytes.length <= Connection.getMaxMsgSize()) {
             tempAttachments.add(
                 new DisputeDirectMessage.Attachment(result.getName(), filesAsBytes));
             inputTextArea.setText(
                 inputTextArea.getText() + "\n[Attachment " + result.getName() + "]");
           } else {
             new Popup().error("The max. allowed file size is 100 kB.").show();
           }
         } catch (java.io.IOException e) {
           e.printStackTrace();
           log.error(e.getMessage());
         }
       } catch (MalformedURLException e2) {
         e2.printStackTrace();
         log.error(e2.getMessage());
       }
     }
   } else {
     new Popup().error("You cannot send more then 3 attachments in one message.").show();
   }
 }
Exemplo n.º 30
0
 @Test
 public void checkBigtestNbt() throws IOException {
   InputStream stream = getClass().getResourceAsStream("bigtest.nbt");
   byte[] data = ByteStreams.toByteArray(stream);
   assertEquals("43629bb139ff0ed46e8dd24f7abbcacf0bc9deededb055d8105b3056ca3fc8df", digest(data));
   stream.close();
 }