View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  import org.eclipse.jetty.client.api.ContentProvider;
26  
27  /**
28   * A {@link ContentProvider} for {@link ByteBuffer}s.
29   * <p />
30   * The position and limit of the {@link ByteBuffer}s passed to the constructor are not modified,
31   * and each invocation of the {@link #iterator()} method returns a {@link ByteBuffer#slice() slice}
32   * of the original {@link ByteBuffer}.
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  }