Example usage for org.apache.commons.vfs.util RandomAccessMode READ

List of usage examples for org.apache.commons.vfs.util RandomAccessMode READ

Introduction

In this page you can find the example usage for org.apache.commons.vfs.util RandomAccessMode READ.

Prototype

RandomAccessMode READ

To view the source code for org.apache.commons.vfs.util RandomAccessMode READ.

Click Source Link

Document

read

Usage

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

/**
 * <p>/*from w w w  .  j av  a2s  .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("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>//  www  .  j ava  2  s . 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);
    }

}

From source file:functionalTests.vfsprovider.TestProActiveProviderAutoclosing.java

@Test
public void testRandomReadOnlyAccessOpenAutocloseRead() throws Exception {
    final FileObject fo = openFileObject(TEST_FILENAME);
    final RandomAccessContent rac = openRandomAccessContent(fo, RandomAccessMode.READ);
    try {//from  ww w . j a  v  a2s  .  c o  m
        Thread.sleep(SLEEP_TIME);
        for (int i = 0; i < TEST_FILE_A_CHARS_NUMBER; i++) {
            assertTrue('a' == rac.readByte());
        }
    } finally {
        rac.close();
    }
    fo.close();
}

From source file:functionalTests.vfsprovider.TestProActiveProviderAutoclosing.java

@Test
public void testRandomReadOnlyAccessOpenReadAutocloseRead() throws Exception {
    final FileObject fo = openFileObject(TEST_FILENAME);
    final RandomAccessContent rac = openRandomAccessContent(fo, RandomAccessMode.READ);
    try {//from  www . j  a  va2  s . c  o m
        for (int i = 0; i < TEST_FILE_A_CHARS_NUMBER; i++) {
            assertTrue('a' == rac.readByte());
        }
        Thread.sleep(SLEEP_TIME);
        for (int i = 0; i < TEST_FILE_B_CHARS_NUMBER; i++) {
            assertTrue('b' == rac.readByte());
        }
    } finally {
        rac.close();
    }
    fo.close();
}

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 www  .  ja v  a  2  s .  c  o m*/
        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 testRandomReadOnlyAccessInputStreamOpenAutocloseRead() throws Exception {
    final FileObject fo = openFileObject(TEST_FILENAME);
    final RandomAccessContent rac = openRandomAccessContent(fo, RandomAccessMode.READ);
    final BufferedReader reader = getBufferedReader(rac.getInputStream());
    try {/*from  w  ww .  j  a va 2s .co  m*/
        Thread.sleep(SLEEP_TIME);
        for (int i = 0; i < TEST_FILE_A_CHARS_NUMBER; i++) {
            assertTrue('a' == reader.read());
        }
    } finally {
        rac.close();
    }
    fo.close();
}

From source file:functionalTests.vfsprovider.TestProActiveProviderAutoclosing.java

@Test
public void testRandomReadOnlyAccessInputStreamOpenReadAutocloseRead() throws Exception {
    final FileObject fo = openFileObject(TEST_FILENAME);
    final RandomAccessContent rac = openRandomAccessContent(fo, RandomAccessMode.READ);
    final BufferedReader reader = getBufferedReader(rac.getInputStream());
    try {//ww  w .  j a  va2s.com
        for (int i = 0; i < TEST_FILE_A_CHARS_NUMBER; i++) {
            assertTrue('a' == reader.read());
        }
        Thread.sleep(SLEEP_TIME);
        for (int i = 0; i < TEST_FILE_B_CHARS_NUMBER; i++) {
            assertTrue('b' == reader.read());
        }
    } finally {
        rac.close();
    }
    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 {/*ww w .  j  a  va 2 s  .  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.newatlanta.appengine.vfs.provider.GaeFileObject.java

@Override
protected InputStream doGetInputStream() throws IOException {
    if (!getType().hasContent()) {
        throw new FileSystemException("vfs.provider/read-not-file.error", getName());
    }//w ww .jav a2 s  .  co  m
    return new GaeRandomAccessContent(this, RandomAccessMode.READ, false).getInputStream();
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadOnlyGetContentRandomInputStream() throws IOException {
    createRealFile();//from  w  w  w. j a v a2  s.c  om

    final FileContent content = readOnlyFile.getContent();
    try {
        content.getRandomAccessContent(RandomAccessMode.READ).close();
    } finally {
        content.close();
    }
}