1 /*
2  * Copyright 2018 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 module hunt.amqp.impl.ProtonReadableBufferImpl;
17 
18 import hunt.io.ByteBuffer;
19 
20 import hunt.proton.codec.ReadableBuffer;
21 import hunt.proton.codec.WritableBuffer;
22 import hunt.text.Charset;
23 import hunt.net.buffer.ByteBuf;
24 
25 /**
26  * Proton ReadableBuffer implementation that wraps a Netty ByteBuf
27  */
28 class ProtonReadableBufferImpl : ReadableBuffer {
29 
30   private ByteBuf buffer;
31 
32   this(ByteBuf buffer) {
33     this.buffer = buffer;
34   }
35 
36   public ByteBuf getBuffer() {
37     return buffer;
38   }
39 
40   
41   public int capacity() {
42     return buffer.capacity();
43   }
44 
45   
46   public bool hasArray() {
47     return buffer.hasArray();
48   }
49 
50   
51   public byte[] array() {
52     return buffer.array();
53   }
54 
55   
56   public int arrayOffset() {
57     return buffer.arrayOffset();
58   }
59 
60   
61   public ReadableBuffer reclaimRead() {
62     return this;
63   }
64 
65   
66   public byte get() {
67     return buffer.readByte();
68   }
69 
70   
71   public byte get(int index) {
72     return buffer.getByte(index);
73   }
74 
75   
76   public int getInt() {
77     return buffer.readInt();
78   }
79 
80   
81   public long getLong() {
82     return buffer.readLong();
83   }
84 
85   
86   public short getShort() {
87     return buffer.readShort();
88   }
89 
90   
91   public float getFloat() {
92     return buffer.readFloat();
93   }
94 
95   
96   public double getDouble() {
97     return buffer.readDouble();
98   }
99 
100   
101   public ReadableBuffer get(byte[] target, int offset, int length) {
102     buffer.readBytes(target, offset, length);
103     return this;
104   }
105 
106   
107   public ReadableBuffer get(byte[] target) {
108     buffer.readBytes(target);
109     return this;
110   }
111 
112   
113   public ReadableBuffer get(WritableBuffer target) {
114     int start = target.position();
115 
116     if (buffer.hasArray()) {
117       target.put(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), buffer.readableBytes());
118     } else {
119       target.put(buffer.nioBuffer());
120     }
121 
122     int written = target.position() - start;
123 
124     buffer.readerIndex(buffer.readerIndex() + written);
125 
126     return this;
127   }
128 
129   
130   public ReadableBuffer slice() {
131     return new ProtonReadableBufferImpl(buffer.slice());
132   }
133 
134   
135   public ReadableBuffer flip() {
136     buffer.setIndex(0, buffer.readerIndex());
137     return this;
138   }
139 
140   
141   public ReadableBuffer limit(int limit) {
142     buffer.writerIndex(limit);
143     return this;
144   }
145 
146   
147   public int limit() {
148     return buffer.writerIndex();
149   }
150 
151   
152   public ReadableBuffer position(int position) {
153     buffer.readerIndex(position);
154     return this;
155   }
156 
157   int opCmp(ReadableBuffer o)
158   {
159       return buffer.readableBytes -  (cast(ProtonReadableBufferImpl)o).buffer.readableBytes ;
160   }
161   
162   public int position() {
163     return buffer.readerIndex();
164   }
165 
166   
167   public ReadableBuffer mark() {
168     buffer.markReaderIndex();
169     return this;
170   }
171 
172   
173   public ReadableBuffer reset() {
174     buffer.resetReaderIndex();
175     return this;
176   }
177 
178   
179   public ReadableBuffer rewind() {
180     buffer.readerIndex(0);
181     return this;
182   }
183 
184   
185   public ReadableBuffer clear() {
186     buffer.setIndex(0, buffer.capacity());
187     return this;
188   }
189 
190   
191   public int remaining() {
192     return buffer.readableBytes();
193   }
194 
195   
196   public bool hasRemaining() {
197     return buffer.isReadable();
198   }
199 
200   
201   public ReadableBuffer duplicate() {
202     return new ProtonReadableBufferImpl(buffer.duplicate());
203   }
204 
205   
206   public ByteBuffer byteBuffer() {
207     return buffer.nioBuffer();
208   }
209 
210   
211   public string readUTF8()  {
212     return buffer.toString(StandardCharsets.UTF_8);
213   }
214 
215   
216   public string readString() {
217     return buffer.toString(StandardCharsets.UTF_8);
218   }
219 }