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.ProtonWritableBufferImpl;
17 
18 import hunt.io.ByteBuffer;
19 
20 import hunt.proton.codec.ReadableBuffer;
21 import hunt.proton.codec.WritableBuffer;
22 import hunt.net.buffer.Unpooled;
23 import hunt.net.buffer.ByteBuf;
24 import  hunt.text.Charset;
25 //import io.netty.buffer.ByteBuf;
26 //import io.netty.buffer.Unpooled;
27 
28 /**
29  * Proton WritableBuffer implementation that wraps a Netty ByteBuf
30  */
31 class ProtonWritableBufferImpl : WritableBuffer {
32 
33   public static  int INITIAL_CAPACITY = 1024;
34 
35   public ByteBuf nettyBuffer;
36 
37   this() {
38     this(INITIAL_CAPACITY);
39   }
40 
41   this(int initialCapacity) {
42     nettyBuffer = Unpooled.buffer(initialCapacity);
43   }
44 
45   this(ByteBuf buffer) {
46     nettyBuffer = buffer;
47   }
48 
49   public ByteBuf getBuffer() {
50     return nettyBuffer;
51   }
52 
53   override
54   public void put(byte b) {
55     nettyBuffer.writeByte(b);
56   }
57 
58   override
59   public void putFloat(float f) {
60     nettyBuffer.writeFloat(f);
61   }
62 
63   override
64   public void putDouble(double d) {
65     nettyBuffer.writeDouble(d);
66   }
67 
68   override
69   public void put(byte[] src, int offset, int length) {
70     nettyBuffer.writeBytes(src, offset, length);
71   }
72 
73   override
74   public void put(ByteBuffer payload) {
75     nettyBuffer.writeBytes(payload);
76   }
77 
78   public void put(ByteBuf payload) {
79     nettyBuffer.writeBytes(payload);
80   }
81 
82   override
83   public void putShort(short s) {
84     nettyBuffer.writeShort(s);
85   }
86 
87   override
88   public void putInt(int i) {
89     nettyBuffer.writeInt(i);
90   }
91 
92   override
93   public void putLong(long l) {
94     nettyBuffer.writeLong(l);
95   }
96 
97   override
98   public bool hasRemaining() {
99     return nettyBuffer.writerIndex() < nettyBuffer.maxCapacity();
100   }
101 
102   override
103   public int remaining() {
104     return nettyBuffer.maxCapacity() - nettyBuffer.writerIndex();
105   }
106 
107   override
108   public void ensureRemaining(int remaining) {
109     nettyBuffer.ensureWritable(remaining);
110   }
111 
112   override
113   public int position() {
114     return nettyBuffer.writerIndex();
115   }
116 
117   override
118   public void position(int position) {
119     nettyBuffer.writerIndex(position);
120   }
121 
122   override
123   public int limit() {
124     return nettyBuffer.capacity();
125   }
126 
127   override
128   public void put(ReadableBuffer buffer) {
129     buffer.get(this);
130   }
131 
132   override
133   public void put(string value) {
134     nettyBuffer.writeCharSequence(value,StandardCharsets.UTF_8);
135   }
136 }