Example usage for org.apache.commons.vfs RandomAccessContent seek

List of usage examples for org.apache.commons.vfs RandomAccessContent seek

Introduction

In this page you can find the example usage for org.apache.commons.vfs RandomAccessContent seek.

Prototype

public void seek(long pos) throws IOException;

Source Link

Document

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Usage

From source file:functionalTests.vfsprovider.TestProActiveProviderAutoclosing.java

@Test
public void testRandomReadOnlyAccessOpenReadSeekAutocloseGetPosRead() throws Exception {
    final FileObject fo = openFileObject(TEST_FILENAME);
    final RandomAccessContent rac = openRandomAccessContent(fo, RandomAccessMode.READ);
    try {/*from  ww  w  .  j  a va 2  s .c  om*/
        rac.readByte();
        rac.seek(TEST_FILE_A_CHARS_NUMBER);
        Thread.sleep(SLEEP_TIME);
        assertEquals(TEST_FILE_A_CHARS_NUMBER, rac.getFilePointer());
        assertTrue('b' == rac.readByte());
    } finally {
        rac.close();
    }
    fo.close();
}

From source file:functionalTests.vfsprovider.TestProActiveProviderAutoclosing.java

@Test
public void testRandomReadWriteAccessOpenWriteSeekAutocloseGetPosWrite() throws Exception {
    final FileObject fo = openFileObject("out.txt");
    fo.createFile();//from   w ww  . j av a 2s  .c  o m
    final RandomAccessContent rac = openRandomAccessContent(fo, RandomAccessMode.READWRITE);
    try {
        rac.write("abcdef".getBytes());
        rac.seek("abc".getBytes().length);
        Thread.sleep(SLEEP_TIME);
        assertEquals("abc".getBytes().length, rac.getFilePointer());
        rac.write("ghi".getBytes());
    } finally {
        rac.close();
    }
    assertContentEquals(fo, "abcghi");
    fo.close();
}

From source file:functionalTests.vfsprovider.TestProActiveProviderAutoclosing.java

@Test
public void testRandomReadOnlyAccessInputStreamOpenSeekAutocloseReadGetPos() throws Exception {
    final FileObject fo = openFileObject(TEST_FILENAME);
    final RandomAccessContent rac = openRandomAccessContent(fo, RandomAccessMode.READ);
    InputStream is = rac.getInputStream();
    try {/*from  w w w  .  j  a  v  a2s.  c o  m*/
        rac.seek(TEST_FILE_A_CHARS_NUMBER);
        // reget input stream
        is = rac.getInputStream();
        Thread.sleep(SLEEP_TIME);
        for (int i = 0; i < TEST_FILE_B_CHARS_NUMBER; i++) {
            assertTrue('b' == is.read());
        }
        assertEquals(TEST_FILE_A_CHARS_NUMBER + TEST_FILE_B_CHARS_NUMBER, rac.getFilePointer());
    } finally {
        rac.close();
    }
    fo.close();
}

From source file:com.adito.vfs.webdav.methods.GET.java

/**
 * <p>// ww w . j  a v a2 s .  co  m
 * Process the <code>GET</code> method.
 * </p>
 */
public void process(DAVTransaction transaction, VFSResource resource) throws LockedException, IOException {

    String handle = VFSLockManager.getNewHandle();
    VFSLockManager.getInstance().lock(resource, transaction.getSessionInfo(), false, true, handle);

    try {
        doHead(transaction, resource);

        try {
            if (resource.isCollection() || resource.isMount()) {

                if (SystemProperties.get("adito.disableFolderBrowsing", "true").equals("true")
                        && !resource.isBrowsable()) {
                    transaction.getResponse().sendError(404);
                    return;
                }

                String mime = COLLECTION_MIME_TYPE + "; charset=\"utf-8\"";
                transaction.setContentType(mime);

                Util.noCache(transaction.getResponse());

                PrintWriter out = transaction.write("utf-8");
                String path = resource.getFullPath();
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Collection: " + path + "</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h2>Collection: " + path + "</h2>");
                out.println("<table>");
                out.println("<thead>");
                out.println("<tr>");
                out.println("<td>Name</td>");
                out.println("<td>Type</td>");
                out.println("<td>Size</td>");
                out.println("<td>Date</td>");
                out.println("</tr>");
                out.println("</thead>");
                out.println("<tbody>");

                /* Process the parent */
                VFSResource parent = resource.getParent();
                if (parent != null && (parent.isBrowsable()
                        || !SystemProperties.get("adito.disableFolderBrowsing", "true").equals("true"))) {
                    out.println("<tr>");
                    out.print("<td><li><a href=\"..\">../</a></td>");
                    out.println("<td>Dir</td><td>");
                    try {
                        out.println(parent.getFile().getContent().getSize());
                    } catch (Exception e) {
                    }
                    out.println("</td><td>");
                    try {
                        out.println(SimpleDateFormat.getDateTimeInstance()
                                .format(new Date(parent.getFile().getContent().getLastModifiedTime())));
                    } catch (Exception e) {
                    }
                    out.println("</td></tr>");
                }

                /* Process the children */
                Iterator iterator = resource.getChildren();
                if (iterator != null) {
                    while (iterator.hasNext()) {
                        VFSResource child = (VFSResource) iterator.next();
                        String childPath = child.getDisplayName();

                        out.println("<tr>");
                        out.print("<td><li><a href=\"" + child.getWebFolderPath() + "\">" + childPath
                                + "</a></td>");
                        if (child.isCollection())
                            out.println("<td>Dir</td><td>");
                        else if (child.isResource())
                            out.println("<td>Resource</td><td>");
                        else
                            out.println("<td>Unknown</td><td>");
                        try {
                            out.println(child.getFile().getContent().getSize());
                        } catch (Exception e) {
                        }
                        out.println("</td><td>");
                        try {
                            out.println(SimpleDateFormat.getDateTimeInstance()
                                    .format(new Date(child.getFile().getContent().getLastModifiedTime())));
                        } catch (Exception e) {
                        }
                        out.println("</td></tr>");
                        out.println("</tr>");
                    }
                }
                out.println("</tbody>");
                out.println("</table>");
                out.println("</html>");
                out.flush();
                if (resource.getMount() != null) {
                    resource.getMount().resourceAccessList(resource, transaction, null);
                }
                return;
            }
        } catch (Exception e) {
            if (resource.getMount() != null) {
                resource.getMount().resourceAccessList(resource, transaction, e);
            }
            IOException ioe = new IOException(e.getMessage());
            ioe.initCause(e);
            throw ioe;
        }

        int total = 0;
        try {

            Range[] ranges = null;

            try {
                ranges = processRangeHeader(transaction, resource);
            } catch (IOException ex) {
                // Invalid range means send full entity with 200 OK.
                ranges = null;
            }

            transaction.setHeader("Content-Type", resource.getContentType());

            resource.getMount().resourceAccessDownloading(resource, transaction);

            OutputStream out = transaction.getOutputStream();
            byte buffer[] = new byte[32768];

            InputStream in = null;

            try {

                if (ranges == null || ranges.length > 1 /* We dont support multiple ranges yet */) {
                    /* Processing a normal resource request */
                    in = resource.getInputStream();
                    int k;
                    while ((k = in.read(buffer)) != -1) {
                        out.write(buffer, 0, k);
                        total += k;
                    }
                } else {
                    /* Process a single range */

                    RandomAccessContent content = resource.getFile().getContent()
                            .getRandomAccessContent(RandomAccessMode.READ);

                    content.seek(ranges[0].startPosition);
                    in = content.getInputStream();

                    long count = ranges[0].count;
                    int k;
                    while (count > 0) {
                        while ((k = in.read(buffer, 0,
                                (int) (count < buffer.length ? count : buffer.length))) > 0) {
                            out.write(buffer, 0, k);
                            total += k;
                            count -= k;
                        }
                    }

                    transaction.setHeader("Content-Range",
                            "bytes " + ranges[0].startPosition + "-" + ranges[0].startPosition + ranges[0].count
                                    + "/" + resource.getFile().getContent().getSize());

                    transaction.setStatus(206 /* Partial */);

                }
            } finally {
                Util.closeStream(in);
            }
            resource.getMount().resourceAccessDownloadComplete(resource, transaction, null);
        } catch (IOException ioe) {
            resource.getMount().resourceAccessDownloadComplete(resource, transaction, ioe);
            throw ioe;
        }
    } finally {
        VFSLockManager.getInstance().unlock(transaction.getSessionInfo(), handle);
    }

}

From source file:com.sslexplorer.vfs.webdav.methods.GET.java

/**
 * <p>//from   w ww . j av  a  2s. c om
 * Process the <code>GET</code> method.
 * </p>
 */
public void process(DAVTransaction transaction, VFSResource resource) throws LockedException, IOException {

    String handle = VFSLockManager.getNewHandle();
    VFSLockManager.getInstance().lock(resource, transaction.getSessionInfo(), false, true, handle);

    try {
        doHead(transaction, resource);

        try {
            if (resource.isCollection() || resource.isMount()) {

                if (SystemProperties.get("sslexplorer.disableFolderBrowsing", "true").equals("true")
                        && !resource.isBrowsable()) {
                    transaction.getResponse().sendError(404);
                    return;
                }

                String mime = COLLECTION_MIME_TYPE + "; charset=\"utf-8\"";
                transaction.setContentType(mime);

                Util.noCache(transaction.getResponse());

                PrintWriter out = transaction.write("utf-8");
                String path = resource.getFullPath();
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Collection: " + path + "</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h2>Collection: " + path + "</h2>");
                out.println("<table>");
                out.println("<thead>");
                out.println("<tr>");
                out.println("<td>Name</td>");
                out.println("<td>Type</td>");
                out.println("<td>Size</td>");
                out.println("<td>Date</td>");
                out.println("</tr>");
                out.println("</thead>");
                out.println("<tbody>");

                /* Process the parent */
                VFSResource parent = resource.getParent();
                if (parent != null && (parent.isBrowsable()
                        || !SystemProperties.get("sslexplorer.disableFolderBrowsing", "true").equals("true"))) {
                    out.println("<tr>");
                    out.print("<td><li><a href=\"..\">../</a></td>");
                    out.println("<td>Dir</td><td>");
                    try {
                        out.println(parent.getFile().getContent().getSize());
                    } catch (Exception e) {
                    }
                    out.println("</td><td>");
                    try {
                        out.println(SimpleDateFormat.getDateTimeInstance()
                                .format(new Date(parent.getFile().getContent().getLastModifiedTime())));
                    } catch (Exception e) {
                    }
                    out.println("</td></tr>");
                }

                /* Process the children */
                Iterator iterator = resource.getChildren();
                if (iterator != null) {
                    while (iterator.hasNext()) {
                        VFSResource child = (VFSResource) iterator.next();
                        String childPath = child.getDisplayName();

                        out.println("<tr>");
                        out.print("<td><li><a href=\"" + child.getWebFolderPath() + "\">" + childPath
                                + "</a></td>");
                        if (child.isCollection())
                            out.println("<td>Dir</td><td>");
                        else if (child.isResource())
                            out.println("<td>Resource</td><td>");
                        else
                            out.println("<td>Unknown</td><td>");
                        try {
                            out.println(child.getFile().getContent().getSize());
                        } catch (Exception e) {
                        }
                        out.println("</td><td>");
                        try {
                            out.println(SimpleDateFormat.getDateTimeInstance()
                                    .format(new Date(child.getFile().getContent().getLastModifiedTime())));
                        } catch (Exception e) {
                        }
                        out.println("</td></tr>");
                        out.println("</tr>");
                    }
                }
                out.println("</tbody>");
                out.println("</table>");
                out.println("</html>");
                out.flush();
                if (resource.getMount() != null) {
                    resource.getMount().resourceAccessList(resource, transaction, null);
                }
                return;
            }
        } catch (Exception e) {
            if (resource.getMount() != null) {
                resource.getMount().resourceAccessList(resource, transaction, e);
            }
            IOException ioe = new IOException(e.getMessage());
            ioe.initCause(e);
            throw ioe;
        }

        int total = 0;
        try {

            Range[] ranges = null;

            try {
                ranges = processRangeHeader(transaction, resource);
            } catch (IOException ex) {
                // Invalid range means send full entity with 200 OK.
                ranges = null;
            }

            transaction.setHeader("Content-Type", resource.getContentType());

            resource.getMount().resourceAccessDownloading(resource, transaction);

            OutputStream out = transaction.getOutputStream();
            byte buffer[] = new byte[32768];

            InputStream in = null;

            try {

                if (ranges == null || ranges.length > 1 /* We dont support multiple ranges yet */) {
                    /* Processing a normal resource request */
                    in = resource.getInputStream();
                    int k;
                    while ((k = in.read(buffer)) != -1) {
                        out.write(buffer, 0, k);
                        total += k;
                    }
                } else {
                    /* Process a single range */

                    RandomAccessContent content = resource.getFile().getContent()
                            .getRandomAccessContent(RandomAccessMode.READ);

                    content.seek(ranges[0].startPosition);
                    in = content.getInputStream();

                    long count = ranges[0].count;
                    int k;
                    while (count > 0) {
                        while ((k = in.read(buffer, 0,
                                (int) (count < buffer.length ? count : buffer.length))) > 0) {
                            out.write(buffer, 0, k);
                            total += k;
                            count -= k;
                        }
                    }

                    transaction.setHeader("Content-Range",
                            "bytes " + ranges[0].startPosition + "-" + ranges[0].startPosition + ranges[0].count
                                    + "/" + resource.getFile().getContent().getSize());

                    transaction.setStatus(206 /* Partial */);

                }
            } finally {
                Util.closeStream(in);
            }
            resource.getMount().resourceAccessDownloadComplete(resource, transaction, null);
        } catch (IOException ioe) {
            resource.getMount().resourceAccessDownloadComplete(resource, transaction, ioe);
            throw ioe;
        }
    } finally {
        VFSLockManager.getInstance().unlock(transaction.getSessionInfo(), handle);
    }

}