1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http.impl.io;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.OutputStream;
32
33 import org.apache.hc.core5.http.StreamClosedException;
34 import org.apache.hc.core5.http.io.SessionOutputBuffer;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38 public class TestContentLengthOutputStream {
39
40 @Test
41 public void testBasics() throws Exception {
42 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
43 final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
44 final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L);
45
46 final byte[] tmp = new byte[10];
47 out.write(tmp, 0, 10);
48 out.write(1);
49 out.write(tmp, 0, 10);
50 out.write(tmp, 0, 10);
51 out.write(tmp);
52 out.write(1);
53 out.write(2);
54 out.flush();
55 out.close();
56 final byte[] data = outputStream.toByteArray();
57 Assert.assertEquals(15, data.length);
58 }
59
60 @Test
61 public void testClose() throws Exception {
62 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
63 final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
64 final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L);
65 out.close();
66 out.close();
67 final byte[] tmp = new byte[10];
68 try {
69 out.write(tmp);
70 Assert.fail("StreamClosedException expected");
71 } catch (final StreamClosedException expected) {
72 }
73 try {
74 out.write(1);
75 Assert.fail("StreamClosedException expected");
76 } catch (final StreamClosedException expected) {
77 }
78 }
79
80 }
81