Пример #1
0
  // Send File
  public void sendFile(String chunkName) throws IOException {
    OutputStream os = null;
    String currentDir = System.getProperty("user.dir");
    chunkName = currentDir + "/src/srcFile/" + chunkName;
    File myFile = new File(chunkName);

    byte[] arrby = new byte[(int) myFile.length()];

    try {
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(arrby, 0, arrby.length);

      os = csocket.getOutputStream();
      System.out.println("Sending File.");
      os.write(arrby, 0, arrby.length);
      os.flush();
      System.out.println("File Sent.");
      //			os.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      //			os.close();
    }
  }
Пример #2
0
 public static String readFile(String filename) {
   String s = "";
   FileInputStream in = null;
   // FileReader in = null;
   try {
     File file = new File(filename);
     byte[] buffer = new byte[(int) file.length()];
     // char[] buffer = new char[(int) file.length()];
     in = new FileInputStream(file);
     // in = new FileReader(file);
     in.read(buffer);
     s = new String(buffer);
     in.close();
   } catch (FileNotFoundException fnfx) {
     System.err.println("File not found: " + fnfx);
   } catch (IOException iox) {
     System.err.println("I/O problems: " + iox);
   } finally {
     if (in != null) {
       try {
         in.close();
       } catch (IOException ignore) {
         // ignore
       }
     }
   }
   return s;
 }
Пример #3
0
  /**
   * Check is text file.
   *
   * @param f file reference.
   * @param emptyOk default value if empty file.
   * @return Is text file.
   */
  public static boolean textFile(File f, boolean emptyOk) {
    if (f.length() == 0) return emptyOk;

    String detected = VisorMimeTypes.getContentType(f);

    for (String mime : TEXT_MIME_TYPE) if (mime.equals(detected)) return true;

    return false;
  }
Пример #4
0
  /**
   * Read block from file.
   *
   * @param file - File to read.
   * @param off - Marker position in file to start read from if {@code -1} read last blockSz bytes.
   * @param blockSz - Maximum number of chars to read.
   * @param lastModified - File last modification time.
   * @return Read file block.
   * @throws IOException In case of error.
   */
  public static VisorFileBlock readBlock(File file, long off, int blockSz, long lastModified)
      throws IOException {
    RandomAccessFile raf = null;

    try {
      long fSz = file.length();
      long fLastModified = file.lastModified();

      long pos = off >= 0 ? off : Math.max(fSz - blockSz, 0);

      // Try read more that file length.
      if (fLastModified == lastModified && fSz != 0 && pos >= fSz)
        throw new IOException(
            "Trying to read file block with wrong offset: " + pos + " while file size: " + fSz);

      if (fSz == 0)
        return new VisorFileBlock(file.getPath(), pos, fLastModified, 0, false, EMPTY_FILE_BUF);
      else {
        int toRead = Math.min(blockSz, (int) (fSz - pos));

        byte[] buf = new byte[toRead];

        raf = new RandomAccessFile(file, "r");

        raf.seek(pos);

        int cntRead = raf.read(buf, 0, toRead);

        if (cntRead != toRead)
          throw new IOException(
              "Count of requested and actually read bytes does not match [cntRead="
                  + cntRead
                  + ", toRead="
                  + toRead
                  + ']');

        boolean zipped = buf.length > 512;

        return new VisorFileBlock(
            file.getPath(), pos, fSz, fLastModified, zipped, zipped ? zipBytes(buf) : buf);
      }
    } finally {
      U.close(raf, null);
    }
  }
Пример #5
0
  /**
   * Finds all files in folder and in it's sub-tree of specified depth.
   *
   * @param file Starting folder
   * @param maxDepth Depth of the tree. If 1 - just look in the folder, no sub-folders.
   * @param filter file filter.
   * @return List of found files.
   */
  public static List<VisorLogFile> fileTree(File file, int maxDepth, @Nullable FileFilter filter) {
    if (file.isDirectory()) {
      File[] files = (filter == null) ? file.listFiles() : file.listFiles(filter);

      if (files == null) return Collections.emptyList();

      List<VisorLogFile> res = new ArrayList<>(files.length);

      for (File f : files) {
        if (f.isFile() && f.length() > 0) res.add(new VisorLogFile(f));
        else if (maxDepth > 1) res.addAll(fileTree(f, maxDepth - 1, filter));
      }

      return res;
    }

    return F.asList(new VisorLogFile(file));
  }
Пример #6
0
  private static String slowStreamCopy(String filename) {
    String s = "";

    FileReader in = null;
    try {
      File file = new File(filename);

      int size = (int) file.length();

      char[] buffer = new char[size];
      in = new FileReader(file);

      // int count = in.read(buffer, 0, size);
      // if (count != -1)

      int count;
      while ((count = in.read(buffer, 0, size)) >= 0) {
        s = new String(buffer, 0, count);
      }
      in.close();
    }
    /*
     * String line; BufferedReader in = null; try { File file = new
     * File(filename); int size = (int)file.length(); if (size > 0) {
     * StringBuffer sb = new StringBuffer(size); in = new BufferedReader(new
     * InputStreamReader(new FileInputStream(filename),"ISO-8859-1")); while
     * ((line = in.readLine()) != null) { sb.append(line); } in.close(); s =
     * sb.toString(); } }
     */
    catch (FileNotFoundException fnfx) {
      System.err.println("File not found: " + fnfx);
    } catch (IOException iox) {
      System.err.println("I/O problems: " + iox);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ignore) {
          // ignore
        }
      }
    }
    return s;
  }
Пример #7
0
  public static void copyURL(String url, String filenameOut, int bufferSize) throws IOException {

    File outFile = new File(filenameOut);

    long start = System.currentTimeMillis();
    String ok = IO.readURLtoFileWithExceptions(url, outFile, bufferSize);
    double took = .001 * (System.currentTimeMillis() - start);

    double len = (double) outFile.length() / (1000 * 1000);

    double rate = len / took;
    System.out.println(
        " copyURL ("
            + url
            + ") took = "
            + took
            + " sec; len= "
            + len
            + " Mbytes; rate = "
            + Format.d(rate, 3)
            + "Mb/sec ok="
            + ok);
  }
Пример #8
0
 private void readFromFile(File file) throws IOException {
   fis = new FileInputStream(file);
   chan = fis.getChannel();
   ByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, (int) file.length());
   readFromBuffer(buf);
 }