comparison src/org/eclipse/jetty/io/AbstractBuffers.java @ 802:3428c60d7cfc

replace jetty jars with source
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Sep 2016 21:15:48 -0600
parents
children 58a9c4a42292
comparison
equal deleted inserted replaced
801:6a21393191c1 802:3428c60d7cfc
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.io;
20
21 import org.eclipse.jetty.io.nio.DirectNIOBuffer;
22 import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
23
24 public abstract class AbstractBuffers implements Buffers
25 {
26 protected final Buffers.Type _headerType;
27 protected final int _headerSize;
28 protected final Buffers.Type _bufferType;
29 protected final int _bufferSize;
30 protected final Buffers.Type _otherType;
31
32 /* ------------------------------------------------------------ */
33 public AbstractBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize, Buffers.Type otherType)
34 {
35 _headerType=headerType;
36 _headerSize=headerSize;
37 _bufferType=bufferType;
38 _bufferSize=bufferSize;
39 _otherType=otherType;
40 }
41
42 /* ------------------------------------------------------------ */
43 /**
44 * @return Returns the buffer size in bytes.
45 */
46 public int getBufferSize()
47 {
48 return _bufferSize;
49 }
50
51 /* ------------------------------------------------------------ */
52 /**
53 * @return Returns the header size in bytes.
54 */
55 public int getHeaderSize()
56 {
57 return _headerSize;
58 }
59
60
61 /* ------------------------------------------------------------ */
62 /**
63 * Create a new header Buffer
64 * @return new Buffer
65 */
66 final protected Buffer newHeader()
67 {
68 switch(_headerType)
69 {
70 case BYTE_ARRAY:
71 return new ByteArrayBuffer(_headerSize);
72 case DIRECT:
73 return new DirectNIOBuffer(_headerSize);
74 case INDIRECT:
75 return new IndirectNIOBuffer(_headerSize);
76 }
77 throw new IllegalStateException();
78 }
79
80 /* ------------------------------------------------------------ */
81 /**
82 * Create a new content Buffer
83 * @return new Buffer
84 */
85 final protected Buffer newBuffer()
86 {
87 switch(_bufferType)
88 {
89 case BYTE_ARRAY:
90 return new ByteArrayBuffer(_bufferSize);
91 case DIRECT:
92 return new DirectNIOBuffer(_bufferSize);
93 case INDIRECT:
94 return new IndirectNIOBuffer(_bufferSize);
95 }
96 throw new IllegalStateException();
97 }
98
99 /* ------------------------------------------------------------ */
100 /**
101 * Create a new content Buffer
102 * @param size
103 * @return new Buffer
104 */
105 final protected Buffer newBuffer(int size)
106 {
107 switch(_otherType)
108 {
109 case BYTE_ARRAY:
110 return new ByteArrayBuffer(size);
111 case DIRECT:
112 return new DirectNIOBuffer(size);
113 case INDIRECT:
114 return new IndirectNIOBuffer(size);
115 }
116 throw new IllegalStateException();
117 }
118
119 /* ------------------------------------------------------------ */
120 /**
121 * @param buffer
122 * @return True if the buffer is the correct type to be a Header buffer
123 */
124 public final boolean isHeader(Buffer buffer)
125 {
126 if (buffer.capacity()==_headerSize)
127 {
128 switch(_headerType)
129 {
130 case BYTE_ARRAY:
131 return buffer instanceof ByteArrayBuffer && !(buffer instanceof IndirectNIOBuffer);
132 case DIRECT:
133 return buffer instanceof DirectNIOBuffer;
134 case INDIRECT:
135 return buffer instanceof IndirectNIOBuffer;
136 }
137 }
138 return false;
139 }
140
141 /* ------------------------------------------------------------ */
142 /**
143 * @param buffer
144 * @return True if the buffer is the correct type to be a Header buffer
145 */
146 public final boolean isBuffer(Buffer buffer)
147 {
148 if (buffer.capacity()==_bufferSize)
149 {
150 switch(_bufferType)
151 {
152 case BYTE_ARRAY:
153 return buffer instanceof ByteArrayBuffer && !(buffer instanceof IndirectNIOBuffer);
154 case DIRECT:
155 return buffer instanceof DirectNIOBuffer;
156 case INDIRECT:
157 return buffer instanceof IndirectNIOBuffer;
158 }
159 }
160 return false;
161 }
162
163 /* ------------------------------------------------------------ */
164 public String toString()
165 {
166 return String.format("%s [%d,%d]", getClass().getSimpleName(), _headerSize, _bufferSize);
167 }
168 }