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