Search in sources :

Example 1 with BlobInputStream

use of com.zimbra.cs.store.BlobInputStream in project zm-mailbox by Zimbra.

the class AbstractExternalStoreManagerTest method testUncachedSubstream.

@Test
public void testUncachedSubstream() throws Exception {
    ParsedMessage pm = ThreaderTest.getRootMessage();
    byte[] mimeBytes = TestUtil.readInputStream(pm.getRawInputStream());
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    StoreManager sm = StoreManager.getInstance();
    Blob blob = sm.storeIncoming(pm.getRawInputStream());
    StagedBlob staged = sm.stage(blob, mbox);
    MailboxBlob mblob = sm.link(staged, mbox, 0, 0);
    mblob = sm.getMailboxBlob(mbox, 0, 0, staged.getLocator());
    Blob localBlob = mblob.getLocalBlob();
    InputStream stream = sm.getContent(localBlob);
    Assert.assertTrue("input stream external", stream instanceof BlobInputStream);
    if (sm instanceof ExternalStoreManager) {
        ((ExternalStoreManager) sm).clearCache();
    }
    blob.getFile().delete();
    Assert.assertFalse(blob.getFile().exists());
    //create new stream spanning the whole blob
    InputStream newStream = ((BlobInputStream) stream).newStream(0, -1);
    Assert.assertNotNull(newStream);
    Assert.assertTrue("stream content = mime content", TestUtil.bytesEqual(mimeBytes, newStream));
}
Also used : Blob(com.zimbra.cs.store.Blob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) Mailbox(com.zimbra.cs.mailbox.Mailbox) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) BlobInputStream(com.zimbra.cs.store.BlobInputStream) InputStream(java.io.InputStream) BlobInputStream(com.zimbra.cs.store.BlobInputStream) StoreManager(com.zimbra.cs.store.StoreManager) Test(org.junit.Test) AbstractStoreManagerTest(com.zimbra.cs.store.AbstractStoreManagerTest) ThreaderTest(com.zimbra.cs.mailbox.ThreaderTest)

Example 2 with BlobInputStream

use of com.zimbra.cs.store.BlobInputStream in project zm-mailbox by Zimbra.

the class TestBlobInputStream method runBlobInputStreamTest.

public void runBlobInputStreamTest() throws Exception {
    String CONTENT = "0123456789";
    createFile(CONTENT);
    BlobInputStream in = new BlobInputStream(mFile, mFile.length());
    // Test reading all content
    String read = getContent(in, 100);
    assertEquals(CONTENT, read);
    checkEof(in);
    in.close();
    // Test reading beginning and end
    in = new BlobInputStream(mFile, mFile.length());
    assertEquals("01234", getContent(in, 5));
    assertEquals("56789", getContent(in, 100));
    checkEof(in);
    in.close();
    // Test invalid start/end
    try {
        in = new BlobInputStream(mFile, mFile.length(), 6L, 5L);
        fail("Test with start=6 and end=5 should not have succeeded.");
    } catch (IOException e) {
    }
    // Test skip
    in = new BlobInputStream(mFile, mFile.length());
    assertEquals(2, in.skip(2));
    assertEquals("23", getContent(in, 2));
    assertEquals(3, in.skip(3));
    assertEquals("7", getContent(in, 1));
    assertEquals(2, in.skip(1000));
    checkEof(in);
    in.close();
    // Test mark
    in = new BlobInputStream(mFile, mFile.length());
    assertTrue(in.markSupported());
    boolean success = true;
    try {
        in.reset();
    } catch (IOException e) {
        success = false;
    }
    assertFalse("reset() should not have succeeded", success);
    assertEquals("012", getContent(in, 3));
    in.mark(3);
    assertEquals("34", getContent(in, 2));
    in.reset();
    assertEquals("34", getContent(in, 2));
    assertEquals("56", getContent(in, 2));
    success = true;
    try {
        in.reset();
    } catch (IOException e) {
        success = false;
    }
    assertFalse("reset() should not have succeeded", success);
    in.close();
    // Test reading a byte array with an offset.
    in = new BlobInputStream(mFile, mFile.length());
    byte[] buf = new byte[5];
    for (int i = 0; i < 5; i++) {
        buf[i] = 57;
    }
    int numRead = in.read(buf, 3, 2);
    assertTrue("Unexpected number of bytes read: " + numRead, numRead == 1 || numRead == 2);
    int[] untouchedIndexes = null;
    if (numRead == 1) {
        assertEquals((byte) '0', buf[3]);
        untouchedIndexes = new int[] { 0, 1, 2, 4 };
    }
    if (numRead == 2) {
        assertEquals((byte) '0', buf[3]);
        assertEquals((byte) '1', buf[4]);
        untouchedIndexes = new int[] { 0, 1, 2 };
    }
    for (int i : untouchedIndexes) {
        assertEquals("Unexpected value at index " + i, 57, buf[i]);
    }
    in.close();
    // Test reading into a byte array.
    in = new BlobInputStream(mFile, mFile.length());
    in.read();
    in.read();
    numRead = in.read(buf);
    assertTrue(numRead > 0);
    assertTrue(numRead <= 5);
    byte[] test = new byte[numRead];
    System.arraycopy(buf, 0, test, 0, numRead);
    assertTrue("23456".startsWith(new String(test)));
    in.close();
    // Test substream - all content
    InputStream sub = in.newStream(0, CONTENT.length());
    assertEquals(CONTENT, getContent(sub, 100));
    checkEof(sub);
    sub.close();
    // Test substream beginning
    sub = in.newStream(0, 5);
    assertEquals("01234", getContent(sub, 100));
    checkEof(sub);
    sub.close();
    // Test substream end
    sub = in.newStream(5, 10);
    assertEquals("56789", getContent(sub, 100));
    checkEof(sub);
    sub.close();
    sub = in.newStream(5, -1);
    assertEquals("56789", getContent(sub, 100));
    checkEof(sub);
    sub.close();
    // Test substream past EOF
    assertEquals(null, in.newStream(5, 11));
    // Test substream middle
    sub = in.newStream(3, 6);
    assertEquals("345", getContent(sub, 100));
    checkEof(sub);
    sub.close();
    // Test substream position
    sub = in.newStream(3, 6);
    assertEquals(0, ((SharedInputStream) sub).getPosition());
    sub.read(new byte[2]);
    assertEquals(2, ((SharedInputStream) sub).getPosition());
    sub.close();
    // Test sub-substream
    InputStream subsub = ((BlobInputStream) sub).newStream(1, 3);
    assertEquals("45", getContent(subsub, 100));
    // Test position after reading 1 character
    in.close();
    in = new BlobInputStream(mFile, mFile.length());
    assertEquals(0, in.getPosition());
    in.read();
    assertEquals(1, in.getPosition());
    in.close();
    // Test reading byte arrays until the end of the file
    in = new BlobInputStream(mFile, mFile.length());
    buf = new byte[4];
    while ((numRead = in.read(buf)) >= 0) {
    }
    in.close();
    mFile.delete();
}
Also used : BlobInputStream(com.zimbra.cs.store.BlobInputStream) SharedInputStream(javax.mail.internet.SharedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) BlobInputStream(com.zimbra.cs.store.BlobInputStream)

Example 3 with BlobInputStream

use of com.zimbra.cs.store.BlobInputStream in project zm-mailbox by Zimbra.

the class TestBlobInputStream method runLargeFileTest.

/**
     * Tests reading a large file.  Exercises the buffering code.
     */
public void runLargeFileTest() throws Exception {
    String content = createFile(5000);
    BlobInputStream in = new BlobInputStream(mFile, mFile.length());
    assertEquals(content, getContent(in, 5000));
    in.close();
    // Test reading 1 char at a time, then a byte array.  This tests
    // the section of BlobInputStream.read(byte[]), where it reads
    // part of the data from the buffer and part from the file.
    in = new BlobInputStream(mFile, mFile.length());
    String firstChunk = getContent(in, 1000);
    assertEquals(content.substring(0, 1000), firstChunk);
    byte[] secondChunk = new byte[2000];
    int numRead = in.read(secondChunk);
    assertTrue(numRead > 0);
    byte[] test = new byte[numRead];
    System.arraycopy(secondChunk, 0, test, 0, numRead);
    assertEquals(content.substring(1000, 1000 + numRead), new String(test));
    int thirdChunkStartPos = 1000 + numRead;
    // Test bug 24715.  Make sure that we don't get IndexOutOfBoundsException
    // when reading another byte[]
    byte[] thirdChunk = new byte[2000];
    numRead = in.read(thirdChunk);
    assertTrue(numRead > 0);
    test = new byte[numRead];
    System.arraycopy(thirdChunk, 0, test, 0, numRead);
    assertEquals(content.substring(thirdChunkStartPos, thirdChunkStartPos + numRead), new String(thirdChunk));
    mFile.delete();
    in.close();
}
Also used : BlobInputStream(com.zimbra.cs.store.BlobInputStream)

Example 4 with BlobInputStream

use of com.zimbra.cs.store.BlobInputStream in project zm-mailbox by Zimbra.

the class ParsedMessage method initialize.

private void initialize(File file, Long receivedDate, boolean indexAttachments) throws IOException, ServiceException {
    if (file == null) {
        throw new IOException("File cannot be null.");
    }
    if (file.length() == 0) {
        throw new IOException("File " + file.getPath() + " is empty.");
    }
    long size;
    if (FileUtil.isGzipped(file)) {
        size = ByteUtil.getDataLength(new GZIPInputStream(new FileInputStream(file)));
    } else {
        size = file.length();
    }
    sharedStream = new BlobInputStream(file, size);
    initialize(receivedDate, indexAttachments);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) IOException(java.io.IOException) BlobInputStream(com.zimbra.cs.store.BlobInputStream) FileInputStream(java.io.FileInputStream)

Example 5 with BlobInputStream

use of com.zimbra.cs.store.BlobInputStream in project zm-mailbox by Zimbra.

the class AbstractExternalStoreManagerTest method testUncachedFile.

@Test
public void testUncachedFile() throws Exception {
    ParsedMessage pm = ThreaderTest.getRootMessage();
    byte[] mimeBytes = TestUtil.readInputStream(pm.getRawInputStream());
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    StoreManager sm = StoreManager.getInstance();
    Blob blob = sm.storeIncoming(pm.getRawInputStream());
    StagedBlob staged = sm.stage(blob, mbox);
    MailboxBlob mblob = sm.link(staged, mbox, 0, 0);
    mblob = sm.getMailboxBlob(mbox, 0, 0, staged.getLocator());
    Blob localBlob = mblob.getLocalBlob();
    InputStream stream = sm.getContent(localBlob);
    Assert.assertTrue("input stream external", stream instanceof BlobInputStream);
    if (sm instanceof ExternalStoreManager) {
        ((ExternalStoreManager) sm).clearCache();
    }
    blob.getFile().delete();
    Assert.assertFalse(blob.getFile().exists());
    //now get it again. this would bomb if it only looked in cache
    stream = sm.getContent(mblob.getLocalBlob());
    Assert.assertTrue("input stream external", stream instanceof ExternalBlobInputStream);
    ExternalBlobInputStream extStream = (ExternalBlobInputStream) stream;
    File file = extStream.getRootFile();
    Assert.assertTrue(file.exists());
    Assert.assertTrue("stream content = mime content", TestUtil.bytesEqual(mimeBytes, stream));
}
Also used : Blob(com.zimbra.cs.store.Blob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) Mailbox(com.zimbra.cs.mailbox.Mailbox) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) BlobInputStream(com.zimbra.cs.store.BlobInputStream) InputStream(java.io.InputStream) BlobInputStream(com.zimbra.cs.store.BlobInputStream) File(java.io.File) StoreManager(com.zimbra.cs.store.StoreManager) Test(org.junit.Test) AbstractStoreManagerTest(com.zimbra.cs.store.AbstractStoreManagerTest) ThreaderTest(com.zimbra.cs.mailbox.ThreaderTest)

Aggregations

BlobInputStream (com.zimbra.cs.store.BlobInputStream)6 Blob (com.zimbra.cs.store.Blob)3 MailboxBlob (com.zimbra.cs.store.MailboxBlob)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 ThreaderTest (com.zimbra.cs.mailbox.ThreaderTest)2 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)2 AbstractStoreManagerTest (com.zimbra.cs.store.AbstractStoreManagerTest)2 StagedBlob (com.zimbra.cs.store.StagedBlob)2 StoreManager (com.zimbra.cs.store.StoreManager)2 Test (org.junit.Test)2 LmtpProtocolException (com.zimbra.common.lmtp.LmtpProtocolException)1 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)1 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)1 ServiceException (com.zimbra.common.service.ServiceException)1 BufferStream (com.zimbra.common.util.BufferStream)1 CopyInputStream (com.zimbra.common.util.CopyInputStream)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 File (java.io.File)1