Example usage for javax.mail.util SharedByteArrayInputStream SharedByteArrayInputStream

List of usage examples for javax.mail.util SharedByteArrayInputStream SharedByteArrayInputStream

Introduction

In this page you can find the example usage for javax.mail.util SharedByteArrayInputStream SharedByteArrayInputStream.

Prototype

public SharedByteArrayInputStream(byte[] buf) 

Source Link

Document

Create a SharedByteArrayInputStream representing the entire byte array.

Usage

From source file:org.apache.james.mailbox.store.ImmutableMailboxMessageTest.java

@Test
public void fullContentMayBeReadMultipleTimes() throws Exception {
    String fullContent = "Subject: Test1 \n\nBody1\n.\n";
    int bodyStartOctet = 16;
    SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(new DefaultMessageId(), new Date(),
            fullContent.length(), bodyStartOctet,
            new SharedByteArrayInputStream(fullContent.getBytes(StandardCharsets.UTF_8)), new Flags(),
            new PropertyBuilder(), TestId.of(1));

    ImmutableMailboxMessage message = messageFactory.from(TestId.of(1), simpleMailboxMessage);

    softly.assertThat(IOUtils.toString(message.getFullContent(), StandardCharsets.UTF_8))
            .isEqualTo(fullContent);//from  w ww  . j  a v a  2  s  . c om
    softly.assertThat(IOUtils.toString(message.getFullContent(), StandardCharsets.UTF_8))
            .isEqualTo(fullContent);
}

From source file:org.apache.james.mailbox.store.ImmutableMailboxMessageTest.java

@Test
public void headersMayBeReadMultipleTimes() throws Exception {
    String fullContent = "Subject: Test1 \n\nBody1\n.\n";
    int bodyStartOctet = 16;
    SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(new DefaultMessageId(), new Date(),
            fullContent.length(), bodyStartOctet,
            new SharedByteArrayInputStream(fullContent.getBytes(StandardCharsets.UTF_8)), new Flags(),
            new PropertyBuilder(), TestId.of(1));

    ImmutableMailboxMessage message = messageFactory.from(TestId.of(1), simpleMailboxMessage);

    String expectedHeaders = "Subject: Test1 \n";
    softly.assertThat(IOUtils.toString(message.getHeaderContent(), StandardCharsets.UTF_8))
            .isEqualTo(expectedHeaders);
    softly.assertThat(IOUtils.toString(message.getHeaderContent(), StandardCharsets.UTF_8))
            .isEqualTo(expectedHeaders);
}

From source file:org.apache.james.mailbox.store.ImmutableMailboxMessageTest.java

@Test
public void bodyMayBeReadMultipleTimes() throws Exception {
    String fullContent = "Subject: Test1 \n\nBody1\n.\n";
    int bodyStartOctet = 16;
    SimpleMailboxMessage simpleMailboxMessage = new SimpleMailboxMessage(new DefaultMessageId(), new Date(),
            fullContent.length(), bodyStartOctet,
            new SharedByteArrayInputStream(fullContent.getBytes(StandardCharsets.UTF_8)), new Flags(),
            new PropertyBuilder(), TestId.of(1));

    ImmutableMailboxMessage message = messageFactory.from(TestId.of(1), simpleMailboxMessage);

    String expectedBody = "\nBody1\n.\n";
    softly.assertThat(IOUtils.toString(message.getBodyContent(), StandardCharsets.UTF_8))
            .isEqualTo(expectedBody);/*w  w  w. j a v a  2  s  . co  m*/
    softly.assertThat(IOUtils.toString(message.getBodyContent(), StandardCharsets.UTF_8))
            .isEqualTo(expectedBody);
}

From source file:org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage.java

private static SharedByteArrayInputStream copyFullContent(MailboxMessage original) throws MailboxException {
    try {/*from w w  w .  ja v  a2  s .  c o  m*/
        return new SharedByteArrayInputStream(IOUtils.toByteArray(original.getFullContent()));
    } catch (IOException e) {
        throw new MailboxException("Unable to parse message", e);
    }
}

From source file:org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessageTest.java

@Test
public void copyShouldReturnFieldByFieldEqualsObject() throws MailboxException {
    long textualLineCount = 42L;
    String text = "text";
    String plain = "plain";
    PropertyBuilder propertyBuilder = new PropertyBuilder();
    propertyBuilder.setTextualLineCount(textualLineCount);
    propertyBuilder.setMediaType(text);/*from  w w w  .j ava2s  . c  om*/
    propertyBuilder.setSubType(plain);
    SimpleMailboxMessage original = new SimpleMailboxMessage(new Date(), MESSAGE_CONTENT.length(),
            BODY_START_OCTET, new SharedByteArrayInputStream(MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET)),
            new Flags(), propertyBuilder, TEST_ID);

    SimpleMailboxMessage copy = SimpleMailboxMessage.copy(TestId.of(1337), original);

    assertThat((Object) copy).isEqualToIgnoringGivenFields(original, "message", "mailboxId")
            .isNotSameAs(original);
    assertThat(copy.getMessage()).usingComparator(new FieldByFieldComparator())
            .isEqualTo(original.getMessage());
    assertThat(SimpleMailboxMessage.copy(TEST_ID, original).getTextualLineCount()).isEqualTo(textualLineCount);
    assertThat(SimpleMailboxMessage.copy(TEST_ID, original).getMediaType()).isEqualTo(text);
    assertThat(SimpleMailboxMessage.copy(TEST_ID, original).getSubType()).isEqualTo(plain);

}

From source file:org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessageTest.java

private static SimpleMailboxMessage buildMessage(String content) {
    return new SimpleMailboxMessage(Calendar.getInstance().getTime(), content.length(), BODY_START_OCTET,
            new SharedByteArrayInputStream(content.getBytes(MESSAGE_CHARSET)), new Flags(),
            new PropertyBuilder(), TEST_ID);
}

From source file:org.apache.james.mailbox.store.mail.model.impl.SimpleMessage.java

public SimpleMessage(Mailbox<Id> mailbox, Message<Id> original) throws MailboxException {
    this.internalDate = original.getInternalDate();
    this.size = original.getFullContentOctets();
    this.mailboxId = mailbox.getMailboxId();
    setFlags(original.createFlags());//from w w w .  j  a va2  s. co  m
    try {
        this.content = new SharedByteArrayInputStream(IOUtils.toByteArray(original.getFullContent()));
    } catch (IOException e) {
        throw new MailboxException("Unable to parse message", e);
    }

    this.bodyStartOctet = (int) (original.getFullContentOctets() - original.getBodyOctets());

    PropertyBuilder pBuilder = new PropertyBuilder(original.getProperties());
    this.lineCount = original.getTextualLineCount();
    this.mediaType = original.getMediaType();
    this.subType = original.getSubType();
    final List<Property> properties = pBuilder.toProperties();
    this.properties = new ArrayList<Property>(properties.size());
    for (final Property property : properties) {
        this.properties.add(new SimpleProperty(property));
    }
}

From source file:org.apache.james.mailbox.store.mail.model.impl.SimpleMessageTest.java

private static SimpleMessage<Long> buildMessage(String content) {
    return new SimpleMessage<Long>(Calendar.getInstance().getTime(), content.length(), 0,
            new SharedByteArrayInputStream(content.getBytes()), new Flags(), new PropertyBuilder(), 1L);
}

From source file:org.apache.james.mailbox.store.mail.model.MessageIdMapperTest.java

private SimpleMailboxMessage createMessage(Mailbox mailbox, String content, int bodyStart,
        PropertyBuilder propertyBuilder) {
    return new SimpleMailboxMessage(mapperProvider.generateMessageId(), new Date(), content.length(), bodyStart,
            new SharedByteArrayInputStream(content.getBytes()), new Flags(), propertyBuilder,
            mailbox.getMailboxId());/* w  ww .  j ava  2 s.  c o m*/
}

From source file:org.apache.james.server.core.MimeMessageInputStreamSource.java

/**
 * Get an input stream to retrieve the data stored in the temporary file
 *
 * @return a <code>BufferedInputStream</code> containing the data
 *//*w  ww  .  ja  va 2 s .c o  m*/
@Override
public synchronized InputStream getInputStream() throws IOException {
    InputStream in;
    if (out.isInMemory()) {
        in = new SharedByteArrayInputStream(out.getData());
    } else {
        in = new SharedFileInputStream(out.getFile());
    }
    streams.add(in);
    return in;
}