View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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  /**
26   * A {@link ContentProvider} for {@link ByteBuffer}s.
27   * <p />
28   * The position and limit of the {@link ByteBuffer}s passed to the constructor are not modified,
29   * and each invocation of the {@link #iterator()} method returns a {@link ByteBuffer#slice() slice}
30   * of the original {@link ByteBuffer}.
31   */
32  public class ByteBufferContentProvider extends AbstractTypedContentProvider
33  {
34      private final ByteBuffer[] buffers;
35      private final int length;
36  
37      public ByteBufferContentProvider(ByteBuffer... buffers)
38      {
39          this("application/octet-stream", buffers);
40      }
41  
42      public ByteBufferContentProvider(String contentType, ByteBuffer... buffers)
43      {
44          super(contentType);
45          this.buffers = buffers;
46          int length = 0;
47          for (ByteBuffer buffer : buffers)
48              length += buffer.remaining();
49          this.length = length;
50      }
51  
52      @Override
53      public long getLength()
54      {
55          return length;
56      }
57  
58      @Override
59      public Iterator<ByteBuffer> iterator()
60      {
61          return new Iterator<ByteBuffer>()
62          {
63              private int index;
64  
65              @Override
66              public boolean hasNext()
67              {
68                  return index < buffers.length;
69              }
70  
71              @Override
72              public ByteBuffer next()
73              {
74                  try
75                  {
76                      ByteBuffer buffer = buffers[index];
77                      buffers[index] = buffer.slice();
78                      ++index;
79                      return buffer;
80                  }
81                  catch (ArrayIndexOutOfBoundsException x)
82                  {
83                      throw new NoSuchElementException();
84                  }
85              }
86  
87              @Override
88              public void remove()
89              {
90                  throw new UnsupportedOperationException();
91              }
92          };
93      }
94  }