public static void unzipIntoDirectory(InputStream inputStream, File directory) {
    if (directory.isFile()) return;
    directory.mkdirs();

    try {
      inputStream = new BufferedInputStream(inputStream);
      inputStream = new ZipInputStream(inputStream);

      for (ZipEntry entry = null;
          (entry = ((ZipInputStream) inputStream).getNextEntry()) != null; ) {
        StringBuilder pathBuilder =
            new StringBuilder(directory.getPath()).append('/').append(entry.getName());
        File file = new File(pathBuilder.toString());

        if (entry.isDirectory()) {
          file.mkdirs();
          continue;
        }

        StreamUtil.write(pathBuilder, inputStream, false);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      StreamUtil.closeQuietly(inputStream);
    }
  }
Esempio n. 2
0
  /** Adds byte content into the zip as a file. */
  public static void addToZip(ZipOutputStream zos, byte[] content, String path, String comment)
      throws IOException {
    while (path.length() != 0 && path.charAt(0) == '/') {
      path = path.substring(1);
    }

    if (StringUtil.endsWithChar(path, '/')) {
      path = path.substring(0, path.length() - 1);
    }

    ZipEntry zipEntry = new ZipEntry(path);
    zipEntry.setTime(System.currentTimeMillis());

    if (comment != null) {
      zipEntry.setComment(comment);
    }

    zos.putNextEntry(zipEntry);

    InputStream is = new ByteArrayInputStream(content);
    try {
      StreamUtil.copy(is, zos);
    } finally {
      StreamUtil.close(is);
    }

    zos.closeEntry();
  }
Esempio n. 3
0
 @Test
 public void testCopy() {
   AsciiInputStream in = new AsciiInputStream("input");
   StringOutputStream out = new StringOutputStream();
   try {
     StreamUtil.copy(in, out);
   } catch (IOException ioex) {
     fail("StreamUtil.copy " + ioex.toString());
   }
   assertEquals("input", out.toString());
   StreamUtil.close(out);
   StreamUtil.close(in);
 }
 int encodedLength() throws IOException {
   if (encoded != null) {
     return 1 + StreamUtil.calculateBodyLength(encoded.length) + encoded.length;
   } else {
     return super.toDLObject().encodedLength();
   }
 }
Esempio n. 5
0
 static {
   final Enumeration<URL> resources;
   String jarName = "(unknown)";
   String versionString = "(unknown)";
   try {
     final ClassLoader classLoader = Main.class.getClassLoader();
     resources =
         classLoader == null
             ? ModuleClassLoader.getSystemResources("META-INF/MANIFEST.MF")
             : classLoader.getResources("META-INF/MANIFEST.MF");
     while (resources.hasMoreElements()) {
       final URL url = resources.nextElement();
       try {
         final InputStream stream = url.openStream();
         if (stream != null)
           try {
             final Manifest manifest = new Manifest(stream);
             final Attributes mainAttributes = manifest.getMainAttributes();
             if (mainAttributes != null
                 && "JBoss Modules".equals(mainAttributes.getValue("Specification-Title"))) {
               jarName = mainAttributes.getValue("Jar-Name");
               versionString = mainAttributes.getValue("Jar-Version");
             }
           } finally {
             StreamUtil.safeClose(stream);
           }
       } catch (IOException ignored) {
       }
     }
   } catch (IOException ignored) {
   }
   JAR_NAME = jarName;
   VERSION_STRING = versionString;
 }
Esempio n. 6
0
 public String httpGet(String urlStr) {
   URL url = null;
   HttpURLConnection conn = null;
   Message msg = handler.obtainMessage();
   ;
   try {
     url = new URL(urlStr);
     conn = (HttpURLConnection) url.openConnection();
     conn.setRequestMethod("GET");
     conn.setConnectTimeout(5000);
     conn.setReadTimeout(5000);
     conn.connect();
     if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
       String result = StreamUtil.getStringFromInputStream(conn.getInputStream());
       return result;
     } else if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
       msg.what = VictorApplication.CODE_URL_NOT_FOUND;
       msg.arg1 = 11111;
       handler.sendMessage(msg);
     }
   } catch (MalformedURLException e) {
     msg.what = VictorApplication.CODE_URL_ERROR;
     msg.arg1 = 11111;
     handler.sendMessage(msg);
     e.printStackTrace();
   } catch (IOException e) {
     msg.what = VictorApplication.CODE_NET_ERROR;
     msg.arg1 = 11111;
     handler.sendMessage(msg);
     e.printStackTrace();
   }
   return null;
 }
Esempio n. 7
0
  /** Decompress gzip archive. */
  public static File ungzip(File file) throws IOException {
    String outFileName = FileNameUtil.removeExtension(file.getAbsolutePath());
    File out = new File(outFileName);
    out.createNewFile();

    FileOutputStream fos = new FileOutputStream(out);
    GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(file));
    try {
      StreamUtil.copy(gzis, fos);
    } finally {
      StreamUtil.close(fos);
      StreamUtil.close(gzis);
    }

    return out;
  }
Esempio n. 8
0
  /** Tests that writing a String to an {@link OutputStream} works as expected. */
  @Test
  public void write() throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    StreamUtil.writeString(output, "hello");

    String written = new String(output.toByteArray(), StandardCharsets.US_ASCII);
    assertEquals("hello\0", written);
  }
Esempio n. 9
0
  /**
   * Extracts zip file to the target directory. If patterns are provided only matched paths are
   * extracted.
   *
   * @param zipFile zip file
   * @param destDir destination directory
   * @param patterns optional wildcard patterns of files to extract, may be <code>null</code>
   */
  public static void unzip(File zipFile, File destDir, String... patterns) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    Enumeration zipEntries = zip.entries();

    while (zipEntries.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) zipEntries.nextElement();
      String entryName = entry.getName();

      if (patterns != null && patterns.length > 0) {
        if (Wildcard.matchPathOne(entryName, patterns) == -1) {
          continue;
        }
      }

      File file = (destDir != null) ? new File(destDir, entryName) : new File(entryName);
      if (entry.isDirectory()) {
        if (!file.mkdirs()) {
          if (file.isDirectory() == false) {
            throw new IOException("Failed to create directory: " + file);
          }
        }
      } else {
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
          if (!parent.mkdirs()) {
            if (file.isDirectory() == false) {
              throw new IOException("Failed to create directory: " + parent);
            }
          }
        }

        InputStream in = zip.getInputStream(entry);
        OutputStream out = null;
        try {
          out = new FileOutputStream(file);
          StreamUtil.copy(in, out);
        } finally {
          StreamUtil.close(out);
          StreamUtil.close(in);
        }
      }
    }

    close(zip);
  }
Esempio n. 10
0
  /** Compresses a file into gzip archive. */
  public static File gzip(File file) throws IOException {
    if (file.isDirectory() == true) {
      throw new IOException("Can't gzip folder");
    }
    FileInputStream fis = new FileInputStream(file);

    String gzipName = file.getAbsolutePath() + GZIP_EXT;

    GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(gzipName));
    try {
      StreamUtil.copy(fis, gzos);
    } finally {
      StreamUtil.close(gzos);
      StreamUtil.close(fis);
    }

    return new File(gzipName);
  }
 private String getUrlContent(CachedUrl url) throws IOException {
   InputStream content = url.getUnfilteredInputStream();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   StreamUtil.copy(content, baos);
   content.close();
   String contentStr = new String(baos.toByteArray());
   baos.close();
   return contentStr;
 }
  int encodedLength() throws IOException {
    if (!empty) {
      ASN1Primitive primitive = obj.toASN1Primitive().toDERObject();
      int length = primitive.encodedLength();

      if (explicit) {
        return StreamUtil.calculateTagLength(tagNo)
            + StreamUtil.calculateBodyLength(length)
            + length;
      } else {
        // header length already in calculation
        length = length - 1;

        return StreamUtil.calculateTagLength(tagNo) + length;
      }
    } else {
      return StreamUtil.calculateTagLength(tagNo) + 1;
    }
  }
Esempio n. 13
0
  @Test
  public void testCompare() {
    try {
      File file = new File(dataRoot, "file/a.txt");
      FileInputStream in1 = new FileInputStream(file);

      String content = "test file\r\n";
      if (file.length() == 10) {
        content = StringUtil.remove(content, '\r');
      }
      AsciiInputStream in2 = new AsciiInputStream(content);
      assertTrue(StreamUtil.compare(in1, in2));
      StreamUtil.close(in2);
      StreamUtil.close(in1);
    } catch (FileNotFoundException e) {
      fail("StreamUtil.testCloneCompare " + e.toString());
    } catch (IOException e) {
      fail("StreamUtil.testCloneCompare " + e.toString());
    }
  }
 public static void copyFile(File from, File into) {
   if (!from.exists()) return;
   if (from.getPath().equals(into.getPath())) return;
   from.setReadable(true);
   if (into.exists()) into.delete();
   try {
     StreamUtil.writeBuffer(into, new FileInputStream(from), true);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Esempio n. 15
0
 /** Verify that using a ByteArrayInputStream does not allocate a new byte array. */
 @Test
 public void testByteArrayInputStream() throws Exception {
   byte[] bytes = new byte[8];
   InputStream input = new ByteArrayInputStream(bytes);
   try {
     byte[] bytesRead = StreamUtil.getBytesFromStream(input);
     assertTrue(Arrays.equals(bytes, bytesRead));
   } finally {
     Closeables.close(input, true);
   }
 }
Esempio n. 16
0
  /** Compresses a file into zlib archive. */
  public static File zlib(File file) throws IOException {
    if (file.isDirectory() == true) {
      throw new IOException("Can't zlib folder");
    }
    FileInputStream fis = new FileInputStream(file);
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);

    String zlibFileName = file.getAbsolutePath() + ZLIB_EXT;

    DeflaterOutputStream dos =
        new DeflaterOutputStream(new FileOutputStream(zlibFileName), deflater);

    try {
      StreamUtil.copy(fis, dos);
    } finally {
      StreamUtil.close(dos);
      StreamUtil.close(fis);
    }

    return new File(zlibFileName);
  }
Esempio n. 17
0
 /** Verify that using an offset with ByteArrayInputStream still produces correct output. */
 @Test
 public void testByteArrayInputStreamWithOffset() throws Exception {
   byte[] bytes = new byte[] {0, 1, 2, 3, 4};
   InputStream input = new ByteArrayInputStream(bytes, 1, 4);
   try {
     byte[] bytesRead = StreamUtil.getBytesFromStream(input);
     byte[] expectedBytes = new byte[] {1, 2, 3, 4};
     assertTrue(Arrays.equals(expectedBytes, bytesRead));
   } finally {
     Closeables.close(input, true);
   }
 }
Esempio n. 18
0
 @Test
 public void testSuccessfulSkip() throws Exception {
   InputStream inputStream = mock(InputStream.class);
   when(inputStream.skip(anyLong())).thenReturn(2L);
   assertEquals(10, StreamUtil.skip(inputStream, 10));
   InOrder order = inOrder(inputStream);
   order.verify(inputStream).skip(10);
   order.verify(inputStream).skip(8);
   order.verify(inputStream).skip(6);
   order.verify(inputStream).skip(4);
   order.verify(inputStream).skip(2);
   verifyNoMoreInteractions(inputStream);
 }
Esempio n. 19
0
  @Test
  public void testGetBytes() {
    try {
      FileInputStream in = new FileInputStream(new File(dataRoot, "file/a.txt"));
      byte[] data = StreamUtil.readBytes(in);
      StreamUtil.close(in);

      String s = new String(data);
      s = StringUtil.remove(s, '\r');
      assertEquals("test file\n", s);

      in = new FileInputStream(new File(dataRoot, "file/a.txt"));
      String str = new String(StreamUtil.readChars(in));
      StreamUtil.close(in);
      str = StringUtil.remove(str, '\r');
      assertEquals("test file\n", str);
    } catch (FileNotFoundException e) {
      fail("StreamUtil.testGetBytes " + e.toString());
    } catch (IOException e) {
      fail("StreamUtil.testGetBytes " + e.toString());
    }
  }
Esempio n. 20
0
 @Test
 public void testUnsuccessfulSkip() throws Exception {
   InputStream inputStream = mock(InputStream.class);
   when(inputStream.skip(anyLong())).thenReturn(3L, 5L, 0L, 6L, 0L);
   when(inputStream.read()).thenReturn(3, -1);
   assertEquals(15, StreamUtil.skip(inputStream, 20));
   InOrder order = inOrder(inputStream);
   order.verify(inputStream).skip(20);
   order.verify(inputStream).skip(17);
   order.verify(inputStream).skip(12);
   order.verify(inputStream).read();
   order.verify(inputStream).skip(11);
   order.verify(inputStream).skip(5);
   order.verify(inputStream).read();
   verifyNoMoreInteractions(inputStream);
 }
Esempio n. 21
0
  public static String getChecksum(Path filePath) throws IOException {
    if (!isValidChecksum(filePath)) {
      return "";
    }

    InputStream fileInputStream = null;

    try {
      fileInputStream = Files.newInputStream(filePath);

      byte[] bytes = DigestUtils.sha1(fileInputStream);

      return Base64.encodeBase64String(bytes);
    } finally {
      StreamUtil.cleanUp(fileInputStream);
    }
  }
Esempio n. 22
0
 public static byte[] readFile(String file) throws JDependException {
   FileInputStream fis = null;
   try {
     fis = new FileInputStream(file);
     return StreamUtil.getData(fis);
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     throw new JDependException(e);
   } finally {
     if (fis != null) {
       try {
         fis.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
  private static LayersConfig getLayersConfig(File repoRoot) {
    File layersList = new File(repoRoot, "layers.conf");
    if (!layersList.exists()) {
      return new LayersConfig();
    }
    Reader reader = null;
    try {
      reader = new InputStreamReader(new FileInputStream(layersList), "UTF-8");
      Properties props = new Properties();
      props.load(reader);

      return new LayersConfig(props);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      StreamUtil.safeClose(reader);
    }
  }
Esempio n. 24
0
  public static String getChecksum(Path filePath) throws IOException {
    if (!Files.exists(filePath)
        || (Files.size(filePath) > PropsValues.SYNC_FILE_CHECKSUM_THRESHOLD_SIZE)) {

      return "";
    }

    InputStream fileInputStream = null;

    try {
      fileInputStream = Files.newInputStream(filePath);

      byte[] bytes = DigestUtils.sha1(fileInputStream);

      return Base64.encodeBase64String(bytes);
    } finally {
      StreamUtil.cleanUp(fileInputStream);
    }
  }
Esempio n. 25
0
 public void testDiskRepairMessage() throws Exception {
   int len = 100 * 1024;
   byte[] repairData = ByteArray.makeRandomBytes(len);
   V3LcapMessage src = makeRepairMessage(repairData);
   assertEquals(len, src.getRepairDataLength());
   assertEquals(V3LcapMessage.EST_ENCODED_HEADER_LENGTH + len, src.getEstimatedEncodedLength());
   InputStream srcStream = src.getInputStream();
   V3LcapMessage copy = new V3LcapMessage(srcStream, tempDir, theDaemon);
   assertEqualMessages(src, copy);
   assertEquals(len, copy.getRepairDataLength());
   assertEquals(V3LcapMessage.EST_ENCODED_HEADER_LENGTH + len, src.getEstimatedEncodedLength());
   InputStream in = copy.getRepairDataInputStream();
   assertTrue(in + "", in instanceof FileInputStream);
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   StreamUtil.copy(in, out);
   byte[] repairCopy = out.toByteArray();
   assertEquals(repairData, repairCopy);
   // ensure that repeated delete doesn't cause error
   copy.delete();
   copy.delete();
 }
Esempio n. 26
0
  public static byte[] getFileData(String fileName) throws JDependException {

    assert fileName != null && fileName.length() != 0;

    FileInputStream fis = null;
    try {
      fis = new FileInputStream(fileName);
      return StreamUtil.getData(fis);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      LogUtil.getInstance(FileUtil.class).systemError("文件[" + fileName + "]没有发现。");
      throw new JDependException("文件[" + fileName + "]读取失败。", e);
    } finally {
      try {
        if (fis != null) {
          fis.close();
        }
      } catch (IOException ignore) {
      }
    }
  }
Esempio n. 27
0
 private static String getServiceName(ClassLoader classLoader, String className)
     throws IOException {
   final InputStream stream = classLoader.getResourceAsStream("META-INF/services/" + className);
   if (stream == null) {
     return null;
   }
   try {
     final BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
     String line;
     while ((line = reader.readLine()) != null) {
       final int i = line.indexOf('#');
       if (i != -1) {
         line = line.substring(0, i);
       }
       line = line.trim();
       if (line.length() == 0) continue;
       return line;
     }
     return null;
   } finally {
     StreamUtil.safeClose(stream);
   }
 }
Esempio n. 28
0
  private void checkFileInputStream(int size) throws IOException {
    byte[] bytesToWrite = new byte[size];
    for (int i = 0; i < size; i++) {
      bytesToWrite[i] = (byte) i; // It's okay to truncate
    }

    File tmpFile = File.createTempFile("streamUtil", "test");
    InputStream input = null;
    OutputStream output = null;
    try {
      output = new FileOutputStream(tmpFile);
      output.write(bytesToWrite);
      output.close();

      input = new FileInputStream(tmpFile);
      byte[] bytesRead = StreamUtil.getBytesFromStream(input);
      assertTrue(Arrays.equals(bytesToWrite, bytesRead));
    } finally {
      Closeables.close(input, true);
      Closeables.close(output, false);
      assertTrue(tmpFile.delete());
    }
  }
Esempio n. 29
0
 @Override
 int encodedLength() {
   return 1 + StreamUtil.calculateBodyLength(bytes.length) + bytes.length;
 }
Esempio n. 30
0
 /** Downloads resource as String. */
 public static String downloadString(String url) throws IOException {
   InputStream inputStream = new URL(url).openStream();
   return new String(StreamUtil.readChars(inputStream));
 }