Skip to content

Commit e76619c

Browse files
committed
fix IString replace method
1 parent d83592f commit e76619c

File tree

5 files changed

+159
-7
lines changed

5 files changed

+159
-7
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/text/ByteArrayStringIso8859.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,17 @@ public ByteArrayStringIso8859 replace(int start, int end, CharSequence replace)
334334
int l = replace.length();
335335
int l2 = end - start + 1;
336336
if (l == l2) {
337-
overwrite(start, replace);
337+
overwrite(this.start + start, replace);
338338
return this;
339339
}
340340
if (l < l2) {
341341
overwrite(this.start + start, replace);
342-
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - end);
342+
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - this.start - end);
343343
this.end -= l2 - l;
344344
return this;
345345
}
346346
enlarge(Math.min(l - l2, 16));
347-
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - end);
347+
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - this.start - end);
348348
overwrite(this.start + start, replace);
349349
this.end += l - l2;
350350
return this;

net.lecousin.core/src/main/java/net/lecousin/framework/text/CharArrayString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ public CharArrayString replace(int start, int end, CharSequence replace) {
316316
}
317317
if (l < l2) {
318318
overwrite(this.start + start, replace);
319-
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - end);
319+
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - this.start - end);
320320
this.end -= l2 - l;
321321
return this;
322322
}
323323
enlarge(Math.min(l - l2, 16));
324-
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - end);
324+
System.arraycopy(chars, this.start + start + l2, chars, this.start + start + l, this.end - this.start - end);
325325
overwrite(this.start + start, replace);
326326
this.end += l - l2;
327327
return this;

net.lecousin.core/src/main/java/net/lecousin/framework/text/IString.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ default String asString() {
174174

175175
/** Convert into an array of character arrays. */
176176
default char[][] asCharacters() {
177-
char[] chars = new char[length()];
177+
int l = length();
178+
if (l == 0) return new char[0][];
179+
char[] chars = new char[l];
178180
fill(chars);
179181
return new char[][] { chars };
180182
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestIOError.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.lecousin.framework.core.test.LCCoreAbstractTest;
1515
import net.lecousin.framework.io.IO;
1616
import net.lecousin.framework.io.IOUtil;
17+
import net.lecousin.framework.io.data.ByteArray;
1718
import net.lecousin.framework.mutable.MutableBoolean;
1819
import net.lecousin.framework.util.ConcurrentCloseable;
1920
import net.lecousin.framework.util.Pair;
@@ -189,6 +190,155 @@ public AsyncSupplier<Long, IOException> getSizeAsync() {
189190

190191
}
191192

193+
/** Return an IOException after first bytes. */
194+
public static class ReadableErrorAfterBeginning extends ConcurrentCloseable<IOException> implements IO.Readable, IO.Readable.Buffered {
195+
196+
public ReadableErrorAfterBeginning(ByteArray beginning) {
197+
this.beginning = beginning;
198+
}
199+
200+
protected IOException error = new IOException("it's normal");
201+
protected ByteArray beginning;
202+
203+
@Override
204+
public String getSourceDescription() {
205+
return getClass().getSimpleName();
206+
}
207+
208+
@Override
209+
public IO getWrappedIO() {
210+
return null;
211+
}
212+
213+
@Override
214+
public void setPriority(byte priority) {
215+
}
216+
217+
@Override
218+
public TaskManager getTaskManager() {
219+
return Threading.getCPUTaskManager();
220+
}
221+
222+
@Override
223+
public int read() throws IOException {
224+
if (!beginning.hasRemaining())
225+
throw error;
226+
return beginning.get() & 0xFF;
227+
}
228+
229+
@Override
230+
public int read(byte[] buffer, int offset, int len) throws IOException {
231+
if (!beginning.hasRemaining())
232+
throw error;
233+
int l = Math.min(len, beginning.remaining());
234+
beginning.get(buffer, offset, l);
235+
return l;
236+
}
237+
238+
@Override
239+
public int readFully(byte[] buffer) throws IOException {
240+
return read(buffer, 0, buffer.length);
241+
}
242+
243+
@Override
244+
public int skip(int skip) throws IOException {
245+
if (!beginning.hasRemaining())
246+
throw error;
247+
int l = Math.min(skip, beginning.remaining());
248+
beginning.moveForward(l);
249+
return l;
250+
}
251+
252+
@Override
253+
public AsyncSupplier<ByteBuffer, IOException> readNextBufferAsync(Consumer<Pair<ByteBuffer, IOException>> ondone) {
254+
if (!beginning.hasRemaining())
255+
return IOUtil.error(error, ondone);
256+
ByteBuffer b = beginning.toByteBuffer();
257+
beginning.goToEnd();
258+
return IOUtil.success(b, ondone);
259+
}
260+
261+
@Override
262+
public ByteBuffer readNextBuffer() throws IOException {
263+
if (!beginning.hasRemaining())
264+
throw error;
265+
ByteBuffer b = beginning.toByteBuffer();
266+
beginning.goToEnd();
267+
return b;
268+
}
269+
270+
@Override
271+
public int readAsync() throws IOException {
272+
return read();
273+
}
274+
275+
@Override
276+
public AsyncSupplier<Integer, IOException> readFullySyncIfPossible(ByteBuffer buffer, Consumer<Pair<Integer, IOException>> ondone) {
277+
if (!beginning.hasRemaining())
278+
return IOUtil.error(error, ondone);
279+
int l = Math.min(beginning.remaining(), buffer.remaining());
280+
buffer.put(beginning.getArray(), beginning.getCurrentArrayOffset(), l);
281+
beginning.moveForward(l);
282+
return IOUtil.success(Integer.valueOf(l), ondone);
283+
}
284+
285+
@Override
286+
public IAsync<IOException> canStartReading() {
287+
return new Async<>(true);
288+
}
289+
290+
@Override
291+
public int readSync(ByteBuffer buffer) throws IOException {
292+
if (!beginning.hasRemaining())
293+
throw error;
294+
int l = Math.min(beginning.remaining(), buffer.remaining());
295+
buffer.put(beginning.getArray(), beginning.getCurrentArrayOffset(), l);
296+
beginning.moveForward(l);
297+
return l;
298+
}
299+
300+
@Override
301+
public AsyncSupplier<Integer, IOException> readAsync(ByteBuffer buffer, Consumer<Pair<Integer, IOException>> ondone) {
302+
return readFullySyncIfPossible(buffer, ondone);
303+
}
304+
305+
@Override
306+
public int readFullySync(ByteBuffer buffer) throws IOException {
307+
return readSync(buffer);
308+
}
309+
310+
@Override
311+
public AsyncSupplier<Integer, IOException> readFullyAsync(ByteBuffer buffer, Consumer<Pair<Integer, IOException>> ondone) {
312+
return readAsync(buffer, ondone);
313+
}
314+
315+
@Override
316+
public long skipSync(long n) throws IOException {
317+
return skip((int)n);
318+
}
319+
320+
@Override
321+
public AsyncSupplier<Long, IOException> skipAsync(long n, Consumer<Pair<Long, IOException>> ondone) {
322+
return IOUtil.skipAsyncUsingSync(this, n, ondone);
323+
}
324+
325+
@Override
326+
public byte getPriority() {
327+
return Task.PRIORITY_NORMAL;
328+
}
329+
330+
@Override
331+
protected IAsync<IOException> closeUnderlyingResources() {
332+
return new Async<>(true);
333+
}
334+
335+
@Override
336+
protected void closeResources(Async<IOException> ondone) {
337+
ondone.unblock();
338+
}
339+
340+
}
341+
192342
protected abstract IO.Readable getReadable(ReadableAlwaysError io) throws Exception;
193343

194344
protected abstract IO.Readable.Buffered getReadableBuffered(ReadableAlwaysError io) throws Exception;

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/text/TestByteArrayStringIso8859Buffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class TestByteArrayStringIso8859Buffer extends TestArrayStringBuffer<Byte
1414

1515
@Override
1616
protected ByteArrayStringIso8859Buffer createString(String s) {
17-
return new ByteArrayStringIso8859Buffer(s);
17+
return s.length() > 0 ? new ByteArrayStringIso8859Buffer(s) : new ByteArrayStringIso8859Buffer();
1818
}
1919

2020
@Override

0 commit comments

Comments
 (0)