Package java.io

Examples of java.io.RandomAccessFile


        return encoding != null ? encoding : "ISO-8859-1";
    }

    public static void sendFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {
        OutputStream out = response.getOutputStream();
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        try {
            long fileSize = raf.length();
            long rangeStart = 0;
            long rangeFinish = fileSize - 1;

            // accept attempts to resume download (if any)
            String range = request.getHeader("Range");
            if (range != null && range.startsWith("bytes=")) {
                String pureRange = range.replaceAll("bytes=", "");
                int rangeSep = pureRange.indexOf("-");

                try {
                    rangeStart = Long.parseLong(pureRange.substring(0, rangeSep));
                    if (rangeStart > fileSize || rangeStart < 0) {
                        rangeStart = 0;
                    }
                } catch (NumberFormatException e) {
                    // ignore the exception, keep rangeStart unchanged
                }

                if (rangeSep < pureRange.length() - 1) {
                    try {
                        rangeFinish = Long.parseLong(pureRange.substring(rangeSep + 1));
                        if (rangeFinish < 0 || rangeFinish >= fileSize) {
                            rangeFinish = fileSize - 1;
                        }
                    } catch (NumberFormatException e) {
                        // ignore the exception
                    }
                }
            }

            // set some headers
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-Length", Long.toString(rangeFinish - rangeStart + 1));
            response.setHeader("Content-Range", "bytes " + rangeStart + "-" + rangeFinish + "/" + fileSize);

            // seek to the requested offset
            raf.seek(rangeStart);

            // send the file
            byte[] buffer = new byte[4096];

            long len;
            int totalRead = 0;
            boolean nomore = false;
            while (true) {
                len = raf.read(buffer);
                if (len > 0 && totalRead + len > rangeFinish - rangeStart + 1) {
                    // read more then required?
                    // adjust the length
                    len = rangeFinish - rangeStart + 1 - totalRead;
                    nomore = true;
                }

                if (len > 0) {
                    out.write(buffer, 0, (int) len);
                    totalRead += len;
                    if (nomore) {
                        break;
                    }
                } else {
                    break;
                }
            }
        } finally {
            raf.close();
        }
    }
View Full Code Here


public class BackwardsFileStream extends InputStream {
    private RandomAccessFile raf;
    private long seekPos;

    public BackwardsFileStream(File file) throws IOException {
        raf = new RandomAccessFile(file, "r");
        seekPos = raf.length();
    }
View Full Code Here

        raf = new RandomAccessFile(file, "r");
        seekPos = raf.length();
    }

    public BackwardsFileStream(File file, long pos) throws IOException {
        raf = new RandomAccessFile(file, "r");
        seekPos = pos;
    }
View Full Code Here

        public RandomAccessFile open() throws FileNotFoundException {
          if(this.fileData == null) {
            this.fileData = fileCache.remove(this.file);
            if (this.fileData == null) {
              this.fileData = new RandomAccessFile(file, "rw"); //$NON-NLS-1$
            }
            }
          return this.fileData;
        }
View Full Code Here

        Map.Entry<Long, FileInfo> entry = storageFiles.floorEntry(fileOffset);
        Assertion.isNotNull(entry);
      FileInfo fileInfo = entry.getValue();
      synchronized (fileInfo) {
        try {
          RandomAccessFile fileAccess = fileInfo.open();
              fileAccess.seek(fileOffset - entry.getKey());
              return fileAccess.read(b, offSet, length);
        } catch (IOException e) {
          throw new TeiidComponentException(e, QueryPlugin.Util.getString("FileStoreageManager.error_reading", fileInfo.file.getAbsoluteFile())); //$NON-NLS-1$
        } finally {
          fileInfo.close();
        }
View Full Code Here

              storageFiles.put(fileOffset, newFileInfo);
              fileInfo = newFileInfo;
          }
      synchronized (fileInfo) {
            try {
              RandomAccessFile fileAccess = fileInfo.open();
                long pointer = fileAccess.length();
                fileAccess.setLength(pointer + length);
                fileAccess.seek(pointer);
                fileAccess.write(bytes, offset, length);
            } catch(IOException e) {
                throw new TeiidComponentException(e, QueryPlugin.Util.getString("FileStoreageManager.error_reading", fileInfo.file.getAbsoluteFile())); //$NON-NLS-1$
            } finally {
              fileInfo.close();
            }
View Full Code Here

      // fill vector with column name
      Vector v = new Vector();
      String hasHeader = dyn.getHeaderrow();
      // int vectorColCounter = 0;

      RandomAccessFile ral = new RandomAccessFile(new File(path), "r");
      String readline = "";

      String tabDelimiter = dyn.getFieldseprator();
      String lineDelimiter = dyn.getLineseprator();
      if (tabDelimiter.equals("")) {
        tabDelimiter = dyn.getTab();
      }
      if (lineDelimiter.equals("")) {
        lineDelimiter = dyn.getLine();
      }

      if (hasHeader.equals("Yes") || (hasHeader.equals("No"))) {
        readline = ral.readLine();
        String[] sFileFieldName = readline.split(tabDelimiter);
        for (int k = 0; k < sFileFieldName.length; k++) {
          v.add(new DDNameValue(sFileFieldName[k], sFileFieldName[k]));
        }
      }
View Full Code Here

            bf.append(']');
            System.arraycopy(bf.getBuffer(), 0, bout, byteRangePosition, bf.size());
        }
        else {
            try {
                raf = new RandomAccessFile(tempFile, "rw");
                int boutLen = (int)raf.length();
                range[range.length - 1] = boutLen - range[range.length - 2];
                ByteBuffer bf = new ByteBuffer();
                bf.append('[');
                for (int k = 0; k < range.length; ++k)
View Full Code Here

    private static void testFile(DataOutputStream out) throws IOException {
        File file = new File("test.txt");
        if (file.exists()) {
            file.delete();
        }
        RandomAccessFile write = new RandomAccessFile(file, "rws");
        // RandomAccessFile write = new RandomAccessFile(file, "rwd");
        int fileSize = 10 * 1024 * 1024;
        IOUtils.setLength(write, fileSize);
        write.seek(0);
        int i = 0;
        FileDescriptor fd = write.getFD();
        while (true) {
            if (write.getFilePointer() >= fileSize) {
                break;
            }
            write.writeBytes(i + "\r\n");
            fd.sync();
            out.writeInt(i);
            out.flush();
            i++;
        }
        write.close();
    }
View Full Code Here

    private static void testFile(String mode, boolean flush) throws Exception {
        System.out.println("Testing RandomAccessFile(.., \"" + mode + "\")...");
        if (flush) {
            System.out.println("  with FileDescriptor.sync()");
        }
        RandomAccessFile file = new RandomAccessFile("test.txt", mode);
        file.setLength(0);
        FileDescriptor fd = file.getFD();
        long start = System.currentTimeMillis();
        byte[] data = { 0 };
        file.write(data);
        int i = 0;
        if (flush) {
            for (;; i++) {
                file.seek(0);
                file.write(data);
                fd.sync();
                if ((i & 15) == 0) {
                    long time = System.currentTimeMillis() - start;
                    if (time > 5000) {
                        break;
                    }
                }
            }
        } else {
            for (;; i++) {
                file.seek(0);
                file.write(data);
                if ((i & 1023) == 0) {
                    long time = System.currentTimeMillis() - start;
                    if (time > 5000) {
                        break;
                    }
                }
            }
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("Time: " + time);
        System.out.println("Operations: " + i);
        System.out.println("Operations/second: " + (i * 1000 / time));
        System.out.println();
        file.close();
        new File("test.txt").delete();
    }
View Full Code Here

TOP

Related Classes of java.io.RandomAccessFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.