Skip to content

Commit 49c78bd

Browse files
committed
Add Buffers clear() methods
1 parent 7a7e8a5 commit 49c78bd

2 files changed

Lines changed: 767 additions & 0 deletions

File tree

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.commons.io;
21+
22+
import java.nio.Buffer;
23+
import java.nio.ByteBuffer;
24+
import java.nio.CharBuffer;
25+
import java.nio.DoubleBuffer;
26+
import java.nio.FloatBuffer;
27+
import java.nio.IntBuffer;
28+
import java.nio.LongBuffer;
29+
import java.nio.ShortBuffer;
30+
import java.util.Arrays;
31+
32+
/**
33+
* Helps use {@link Buffer} instances.
34+
*
35+
* @since 2.23.0
36+
*/
37+
public final class Buffers {
38+
39+
/**
40+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
41+
*
42+
* @param buffer the buffer to clear, may be null.
43+
* @return the given buffer.
44+
*/
45+
public static Buffer clear(final Buffer buffer) {
46+
if (buffer instanceof CharBuffer) {
47+
return clear((CharBuffer) buffer);
48+
}
49+
if (buffer instanceof ByteBuffer) {
50+
return clear((ByteBuffer) buffer);
51+
}
52+
if (buffer instanceof DoubleBuffer) {
53+
return clear((DoubleBuffer) buffer);
54+
}
55+
if (buffer instanceof FloatBuffer) {
56+
return clear((FloatBuffer) buffer);
57+
}
58+
if (buffer instanceof IntBuffer) {
59+
return clear((IntBuffer) buffer);
60+
}
61+
if (buffer instanceof LongBuffer) {
62+
return clear((LongBuffer) buffer);
63+
}
64+
if (buffer instanceof ShortBuffer) {
65+
return clear((ShortBuffer) buffer);
66+
}
67+
if (buffer == null) {
68+
return null;
69+
}
70+
throw new UnsupportedOperationException(buffer.getClass().getCanonicalName());
71+
}
72+
73+
/**
74+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
75+
*
76+
* @param buffer the buffer to clear, may be null.
77+
* @return the given buffer.
78+
*/
79+
public static ByteBuffer clear(final ByteBuffer buffer) {
80+
buffer.clear();
81+
if (buffer.hasArray()) {
82+
Arrays.fill(buffer.array(), (byte) 0);
83+
} else {
84+
final byte[] zeros = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
85+
while (buffer.remaining() > 0) {
86+
buffer.put(zeros, 0, Math.min(buffer.remaining(), zeros.length));
87+
}
88+
}
89+
buffer.clear();
90+
return buffer;
91+
}
92+
93+
/**
94+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
95+
*
96+
* @param buffer the buffer to clear, may be null.
97+
* @return the given buffer.
98+
*/
99+
public static CharBuffer clear(final CharBuffer buffer) {
100+
buffer.clear();
101+
if (buffer.hasArray()) {
102+
Arrays.fill(buffer.array(), (char) 0);
103+
} else {
104+
final char[] zeros = new char[IOUtils.DEFAULT_BUFFER_SIZE];
105+
while (buffer.remaining() > 0) {
106+
buffer.put(zeros, 0, Math.min(buffer.remaining(), zeros.length));
107+
}
108+
}
109+
buffer.clear();
110+
return buffer;
111+
}
112+
113+
/**
114+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
115+
*
116+
* @param buffer the buffer to clear, may be null.
117+
* @return the given buffer.
118+
*/
119+
public static DoubleBuffer clear(final DoubleBuffer buffer) {
120+
buffer.clear();
121+
if (buffer.hasArray()) {
122+
Arrays.fill(buffer.array(), 0);
123+
} else {
124+
final double[] zeros = new double[IOUtils.DEFAULT_BUFFER_SIZE];
125+
while (buffer.remaining() > 0) {
126+
buffer.put(zeros, 0, Math.min(buffer.remaining(), zeros.length));
127+
}
128+
}
129+
buffer.clear();
130+
return buffer;
131+
}
132+
133+
/**
134+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
135+
*
136+
* @param buffer the buffer to clear, may be null.
137+
* @return the given buffer.
138+
*/
139+
public static FloatBuffer clear(final FloatBuffer buffer) {
140+
buffer.clear();
141+
if (buffer.hasArray()) {
142+
Arrays.fill(buffer.array(), 0);
143+
} else {
144+
final float[] zeros = new float[IOUtils.DEFAULT_BUFFER_SIZE];
145+
while (buffer.remaining() > 0) {
146+
buffer.put(zeros, 0, Math.min(buffer.remaining(), zeros.length));
147+
}
148+
}
149+
buffer.clear();
150+
return buffer;
151+
}
152+
153+
/**
154+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
155+
*
156+
* @param buffer the buffer to clear, may be null.
157+
* @return the given buffer.
158+
*/
159+
public static IntBuffer clear(final IntBuffer buffer) {
160+
buffer.clear();
161+
if (buffer.hasArray()) {
162+
Arrays.fill(buffer.array(), 0);
163+
} else {
164+
final int[] zeros = new int[IOUtils.DEFAULT_BUFFER_SIZE];
165+
while (buffer.remaining() > 0) {
166+
buffer.put(zeros, 0, Math.min(buffer.remaining(), zeros.length));
167+
}
168+
}
169+
buffer.clear();
170+
return buffer;
171+
}
172+
173+
/**
174+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
175+
*
176+
* @param buffer the buffer to clear, may be null.
177+
* @return the given buffer.
178+
*/
179+
public static LongBuffer clear(final LongBuffer buffer) {
180+
buffer.clear();
181+
if (buffer.hasArray()) {
182+
Arrays.fill(buffer.array(), 0);
183+
} else {
184+
final long[] zeros = new long[IOUtils.DEFAULT_BUFFER_SIZE];
185+
while (buffer.remaining() > 0) {
186+
buffer.put(zeros, 0, Math.min(buffer.remaining(), zeros.length));
187+
}
188+
}
189+
buffer.clear();
190+
return buffer;
191+
}
192+
193+
/**
194+
* Clears the given buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
195+
*
196+
* @param buffer the buffer to clear, may be null.
197+
* @return the given buffer.
198+
*/
199+
public static ShortBuffer clear(final ShortBuffer buffer) {
200+
buffer.clear();
201+
if (buffer.hasArray()) {
202+
Arrays.fill(buffer.array(), (short) 0);
203+
} else {
204+
final short[] zeros = new short[IOUtils.DEFAULT_BUFFER_SIZE];
205+
while (buffer.remaining() > 0) {
206+
buffer.put(zeros, 0, Math.min(buffer.remaining(), zeros.length));
207+
}
208+
}
209+
buffer.clear();
210+
return buffer;
211+
}
212+
213+
/**
214+
* Clears the given <em>direct</em> buffer by filling it with zeros and resetting the position to zero. The limit is set to the capacity of the buffer.
215+
* <p>
216+
* If the given buffer is not direct, nothing happens to that buffer.
217+
* </p>
218+
*
219+
* @param buffer the buffer to clear, may be null.
220+
* @return the given buffer.
221+
*/
222+
public static Buffer clearDirect(final Buffer buffer) {
223+
return buffer != null && buffer.isDirect() ? clear(buffer) : buffer;
224+
}
225+
226+
/**
227+
* No instances.
228+
*/
229+
private Buffers() {
230+
// empty.
231+
}
232+
}

0 commit comments

Comments
 (0)