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.spdy.api;
20  
21  import java.nio.ByteBuffer;
22  
23  /**
24   * <p>Specialized {@link DataInfo} for byte array content.</p>
25   */
26  public class BytesDataInfo extends DataInfo
27  {
28      private final byte[] bytes;
29      private final int offset;
30      private final int length;
31      private int index;
32  
33      public BytesDataInfo(byte[] bytes, boolean close)
34      {
35          this(bytes, 0, bytes.length, close);
36      }
37  
38      public BytesDataInfo(byte[] bytes, int offset, int length, boolean close)
39      {
40          super(close, false);
41          this.bytes = bytes;
42          this.offset = offset;
43          this.length = length;
44          this.index = offset;
45      }
46  
47      @Override
48      public int length()
49      {
50          return length;
51      }
52  
53      @Override
54      public int available()
55      {
56          return length - index + offset;
57      }
58  
59      @Override
60      public int readInto(ByteBuffer output)
61      {
62          int space = output.remaining();
63          int chunk = Math.min(available(), space);
64          output.put(bytes, index, chunk);
65          index += chunk;
66          return chunk;
67      }
68  }