Java Code Examples for org.alfresco.service.cmr.repository.ContentReader#getFileChannel()

The following examples show how to use org.alfresco.service.cmr.repository.ContentReader#getFileChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractReadOnlyContentStoreTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Tests random access reading
 * <p>
 * Only executes if the reader implements {@link RandomAccessContent}.
 */
@Test
public void testRandomAccessRead() throws Exception
{
    ContentStore store = getStore();
    String contentUrl = getExistingContentUrl();
    if (contentUrl == null)
    {
        logger.warn("Store test testRandomAccessRead not possible on " + store.getClass().getName());
        return;
    }
    // Get the reader
    ContentReader reader = store.getReader(contentUrl);
    assertNotNull("Reader should never be null", reader);

    FileChannel fileChannel = reader.getFileChannel();
    assertNotNull("No channel given", fileChannel);
    
    // check that no other content access is allowed
    try
    {
        reader.getReadableChannel();
        fail("Second channel access allowed");
    }
    catch (RuntimeException e)
    {
        // expected
    }
    fileChannel.close();
}
 
Example 2
Source File: AbstractWritableContentStoreTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Tests random access reading
 * <p>
 * Only executes if the reader implements {@link RandomAccessContent}.
 */
@Test
public void testRandomAccessRead() throws Exception
{
    ContentWriter writer = getWriter();
    // put some content
    String content = "ABC";
    byte[] bytes = content.getBytes();
    writer.putContent(content);
    ContentReader reader = writer.getReader();
    
    FileChannel fileChannel = reader.getFileChannel();
    assertNotNull("No channel given", fileChannel);
    
    // check that no other content access is allowed
    try
    {
        reader.getReadableChannel();
        fail("Second channel access allowed");
    }
    catch (RuntimeException e)
    {
        // expected
    }
    
    // read the content
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    int count = fileChannel.read(buffer);
    assertEquals("Incorrect number of bytes read", bytes.length, count);
    // transfer back to array
    ((Buffer) buffer).rewind();
    buffer.get(bytes);
    String checkContent = new String(bytes);
    assertEquals("Content read failure", content, checkContent);
    fileChannel.close();
}
 
Example 3
Source File: HttpAlfrescoStore.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void doTest(ApplicationContext ctx, String baseUrl, String contentUrl) throws Exception
{
    ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    TransactionService transactionService = serviceRegistry.getTransactionService();
    AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
    // Construct the store
    HttpAlfrescoStore store = new HttpAlfrescoStore();
    store.setTransactionService(transactionService);
    store.setAuthenticationService(authenticationService);
    store.setBaseHttpUrl(baseUrl);
    
    // Now test
    System.out.println(
            "   Retrieving reader for URL " + contentUrl);
    ContentReader reader = store.getReader(contentUrl);
    System.out.println(
            "   Retrieved reader for URL " + contentUrl);
    // Check if the content exists
    boolean exists = reader.exists();
    if (!exists)
    {
        System.out.println(
                "   Content doesn't exist: " + contentUrl);
        return;
    }
    else
    {
        System.out.println(
                "   Content exists: " + contentUrl);
    }
    // Get the content data
    ContentData contentData = reader.getContentData();
    System.out.println(
            "   Retrieved content data: " + contentData);
    
    // Now get the content
    ByteBuffer buffer = ByteBuffer.allocate((int)reader.getSize());
    FileChannel channel = reader.getFileChannel();
    try
    {
        int count = channel.read(buffer);
        if (count != reader.getSize())
        {
            System.err.println("The number of bytes read was " + count + " but expected " + reader.getSize());
            return;
        }
    }
    finally
    {
        channel.close();
    }
}