1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.client.util;
20
21 import java.nio.ByteBuffer;
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
24
25 import org.eclipse.jetty.client.api.ContentProvider;
26
27
28
29
30
31
32
33
34 public class ByteBufferContentProvider implements ContentProvider
35 {
36 private final ByteBuffer[] buffers;
37 private final int length;
38
39 public ByteBufferContentProvider(ByteBuffer... buffers)
40 {
41 this.buffers = buffers;
42 int length = 0;
43 for (ByteBuffer buffer : buffers)
44 length += buffer.remaining();
45 this.length = length;
46 }
47
48 @Override
49 public long getLength()
50 {
51 return length;
52 }
53
54 @Override
55 public Iterator<ByteBuffer> iterator()
56 {
57 return new Iterator<ByteBuffer>()
58 {
59 private int index;
60
61 @Override
62 public boolean hasNext()
63 {
64 return index < buffers.length;
65 }
66
67 @Override
68 public ByteBuffer next()
69 {
70 try
71 {
72 ByteBuffer buffer = buffers[index];
73 buffers[index] = buffer.slice();
74 ++index;
75 return buffer;
76 }
77 catch (ArrayIndexOutOfBoundsException x)
78 {
79 throw new NoSuchElementException();
80 }
81 }
82
83 @Override
84 public void remove()
85 {
86 throw new UnsupportedOperationException();
87 }
88 };
89 }
90 }