@@ -99,6 +99,15 @@ public short getShort() {
9999 return byteBuffer .getShort (index );
100100 }
101101
102+ public String getString () {
103+ int lenght = getInt ();
104+ byte [] bytes = new byte [lenght ];
105+ for (int i = 0 ; i < lenght ; i ++) {
106+ bytes [i ] = get ();
107+ }
108+ return new String (bytes );
109+ }
110+
102111 public ByteOrder order () {
103112 return byteBuffer .order ();
104113 }
@@ -107,63 +116,98 @@ public ByteBuffer order(ByteOrder bo) {
107116 return byteBuffer .order (bo );
108117 }
109118
110- public ByteBuffer put (byte b ) {
119+ public void put (byte b ) {
111120 ensureSpace (1 + 1 );
112121 int index = this .writeIndex ;
113122 this .writeIndex += 1 ;
114- return byteBuffer .put (index , b );
123+ byteBuffer .put (index , b );
115124 }
116125
117- public ByteBuffer put (byte [] src ) {
126+ public void put (byte [] src ) {
118127 ensureSpace (src .length );
119- return byteBuffer .put (src );
128+ byteBuffer .put (src );
120129 }
121130
122- public ByteBuffer put (ByteBuffer src ) {
131+ public void put (ByteBuffer src ) {
123132 ensureSpace (src .remaining ());
124- return byteBuffer .put (src );
133+ byteBuffer .put (src );
125134 }
126135
127- public ByteBuffer putChar (char value ) {
136+ public void putChar (char value ) {
128137 ensureSpace (2 + 1 );
129138 int index = this .writeIndex ;
130139 this .writeIndex += 2 ;
131- return byteBuffer .putChar (index , value );
140+ byteBuffer .putChar (index , value );
132141 }
133142
134- public ByteBuffer putDouble (double value ) {
143+ public void putDouble (double value ) {
135144 ensureSpace (8 + 1 );
136145 int index = this .writeIndex ;
137146 this .writeIndex += 8 ;
138- return byteBuffer .putDouble (index , value );
147+ byteBuffer .putDouble (index , value );
139148 }
140149
141- public ByteBuffer putFloat (float value ) {
150+ public void putFloat (float value ) {
142151 ensureSpace (4 + 1 );
143152 int index = this .writeIndex ;
144153 this .writeIndex += 4 ;
145- return byteBuffer .putFloat (index , value );
154+ byteBuffer .putFloat (index , value );
146155 }
147156
148- public ByteBuffer putInt (int value ) {
157+ public void putInt (int value ) {
149158 ensureSpace (4 + 1 );
150159 int index = this .writeIndex ;
151160 this .writeIndex += 4 ;
152- return byteBuffer .putInt (index , value );
161+ byteBuffer .putInt (index , value );
153162 }
154163
155- public ByteBuffer putLong (long value ) {
164+ public void putLong (long value ) {
156165 ensureSpace (8 + 1 );
157166 int index = this .writeIndex ;
158167 this .writeIndex += 8 ;
159- return byteBuffer .putLong (index , value );
168+ byteBuffer .putLong (index , value );
160169 }
161170
162- public ByteBuffer putShort (short value ) {
171+ public void putShort (short value ) {
163172 ensureSpace (2 + 1 );
164173 int index = this .writeIndex ;
165174 this .writeIndex += 2 ;
166- return byteBuffer .putShort (index , value );
175+ byteBuffer .putShort (index , value );
176+ }
177+
178+ public void putString (String value ) {
179+ putInt (value .length ());
180+ for (byte b : value .getBytes ()) {
181+ put (b );
182+ }
183+ }
184+
185+ public static void main (String [] args ) {
186+ DynamicByteBuffer dynamicByteBuffer = new DynamicByteBuffer ();
187+ /*dynamicByteBuffer.putString("Hello World!");
188+ dynamicByteBuffer.putString("Bye World!");
189+ System.out.println(dynamicByteBuffer.getString());
190+ System.out.println(dynamicByteBuffer.getString());*/
191+ dynamicByteBuffer .putInt (0 );
192+ dynamicByteBuffer .putInt (1 );
193+ dynamicByteBuffer .putInt (2 );
194+ dynamicByteBuffer .putInt (3 );
195+ dynamicByteBuffer .putInt (4 );
196+ dynamicByteBuffer .putInt (5 );
197+ dynamicByteBuffer .putInt (6 );
198+ dynamicByteBuffer .putInt (7 );
199+ dynamicByteBuffer .putInt (8 );
200+ dynamicByteBuffer .putInt (9 );
201+ System .out .println (dynamicByteBuffer .getInt ());
202+ System .out .println (dynamicByteBuffer .getInt ());
203+ System .out .println (dynamicByteBuffer .getInt ());
204+ System .out .println (dynamicByteBuffer .getInt ());
205+ System .out .println (dynamicByteBuffer .getInt ());
206+ System .out .println (dynamicByteBuffer .getInt ());
207+ System .out .println (dynamicByteBuffer .getInt ());
208+ System .out .println (dynamicByteBuffer .getInt ());
209+ System .out .println (dynamicByteBuffer .getInt ());
210+ System .out .println (dynamicByteBuffer .getInt ());
167211 }
168212
169213 @ Override
0 commit comments