public static void main(String[] args) throws IOException {
   File file1 = new File("d:" + File.separator + "hello1.txt");
   File file2 = new File("d:" + File.separator + "hello2.txt");
   File file3 = new File("d:" + File.separator + "hello.txt");
   InputStream input1 = new FileInputStream(file1);
   InputStream input2 = new FileInputStream(file2);
   OutputStream output = new FileOutputStream(file3);
   // 合并流
   SequenceInputStream sis = new SequenceInputStream(input1, input2);
   int temp = 0;
   while ((temp = sis.read()) != -1) {
     output.write(temp);
   }
   input1.close();
   input2.close();
   output.close();
   sis.close();
 }
Exemple #2
0
  public static void mergeFile_2(File dir) throws IOException {

    /*
     * 获取指定目录下的配置文件对象。
     */
    File[] files = dir.listFiles(new SuffixFilter(".properties"));
    if (files.length != 1) throw new RuntimeException(dir + ",该目录下没有properties扩展名的文件或者不唯一");
    // 记录配置文件对象。
    File confile = files[0];

    // 获取该文件中的信息。================================================
    FileInputStream fis = new FileInputStream(confile);
    Properties prop = new Properties();
    prop.load(fis);
    String filename = prop.getProperty("filename");
    int count = Integer.parseInt(prop.getProperty("partcount"));

    // 获取该目录下的所有碎片文件。 ==============================================
    File[] partfiles = dir.listFiles(new SuffixFilter(".part"));
    if (partfiles.length != (count - 1))
      throw new RuntimeException("碎片文件不符合要求,个数不对!应该" + count + "个");

    // 将碎片文件和流对象关联 并存储到集合中。
    ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
    for (int x = 0; x < partfiles.length; x++) {
      al.add(new FileInputStream(partfiles[x]));
    }

    // 将多个流合并成一个序列流。
    Enumeration<FileInputStream> en = Collections.enumeration(al);
    SequenceInputStream sis = new SequenceInputStream(en);
    FileOutputStream fos = new FileOutputStream(new File(dir, filename));

    byte[] buf = new byte[1024];
    int len = 0;
    while ((len = sis.read(buf)) != -1) {
      fos.write(buf, 0, len);
    }

    fos.close();
    sis.close();
  }
Exemple #3
0
  public static void mergeFile(File dir) throws IOException {

    ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
    for (int x = 1; x <= 3; x++) {
      al.add(new FileInputStream(new File(dir, x + ".part")));
    }

    Enumeration<FileInputStream> en = Collections.enumeration(al);
    SequenceInputStream sis = new SequenceInputStream(en);

    FileOutputStream fos = new FileOutputStream(new File(dir, "1.bmp"));
    byte[] buf = new byte[1024];
    int len = 0;
    while ((len = sis.read(buf)) != -1) {
      fos.write(buf, 0, len);
    }

    fos.close();
    sis.close();
  }
Exemple #4
0
  /**
   * Uses the given {@link net.i2p.data.SigningPrivateKey} to sign the given input file along with
   * its version string using DSA. The output will be a signed update file where the first 40 bytes
   * are the resulting DSA signature, the next 16 bytes are the input file's version string encoded
   * in UTF-8 (padded with trailing <code>0h</code> characters if necessary), and the remaining
   * bytes are the raw bytes of the input file.
   *
   * @param inputFile The file to be signed.
   * @param signedFile The signed update file to write.
   * @param signingPrivateKey An instance of <code>SigningPrivateKey</code> to sign <code>inputFile
   *     </code> with.
   * @param version The version string of the input file. If this is longer than 16 characters it
   *     will be truncated.
   * @return An instance of {@link net.i2p.data.Signature}, or <code>null</code> if there was an
   *     error.
   */
  public Signature sign(
      String inputFile, String signedFile, SigningPrivateKey signingPrivateKey, String version) {
    byte[] versionHeader = {
      0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00
    };
    byte[] versionRawBytes = null;

    if (version.length() > VERSION_BYTES) version = version.substring(0, VERSION_BYTES);

    try {
      versionRawBytes = version.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("wtf, your JVM doesnt support utf-8? " + e.getMessage());
    }

    System.arraycopy(versionRawBytes, 0, versionHeader, 0, versionRawBytes.length);

    FileInputStream fileInputStream = null;
    Signature signature = null;
    SequenceInputStream bytesToSignInputStream = null;
    ByteArrayInputStream versionHeaderInputStream = null;

    try {
      fileInputStream = new FileInputStream(inputFile);
      versionHeaderInputStream = new ByteArrayInputStream(versionHeader);
      bytesToSignInputStream = new SequenceInputStream(versionHeaderInputStream, fileInputStream);
      signature = _context.dsa().sign(bytesToSignInputStream, signingPrivateKey);

    } catch (Exception e) {
      if (_log.shouldLog(Log.ERROR)) _log.error("Error signing", e);

      return null;
    } finally {
      if (bytesToSignInputStream != null)
        try {
          bytesToSignInputStream.close();
        } catch (IOException ioe) {
        }

      fileInputStream = null;
    }

    FileOutputStream fileOutputStream = null;

    try {
      fileOutputStream = new FileOutputStream(signedFile);
      fileOutputStream.write(signature.getData());
      fileOutputStream.write(versionHeader);
      fileInputStream = new FileInputStream(inputFile);
      byte[] buffer = new byte[1024];
      int bytesRead = 0;
      while ((bytesRead = fileInputStream.read(buffer)) != -1)
        fileOutputStream.write(buffer, 0, bytesRead);
      fileOutputStream.close();
    } catch (IOException ioe) {
      if (_log.shouldLog(Log.WARN))
        _log.log(Log.WARN, "Error writing signed file " + signedFile, ioe);

      return null;
    } finally {
      if (fileInputStream != null)
        try {
          fileInputStream.close();
        } catch (IOException ioe) {
        }

      if (fileOutputStream != null)
        try {
          fileOutputStream.close();
        } catch (IOException ioe) {
        }
    }

    return signature;
  }