Skip to content

Commit b4f2523

Browse files
committed
improve LinkedIO.getSizeAsync
1 parent aca1b3e commit b4f2523

File tree

1 file changed

+15
-24
lines changed
  • net.lecousin.core/src/main/java/net/lecousin/framework/io

1 file changed

+15
-24
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/io/LinkedIO.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import net.lecousin.framework.concurrent.synch.SynchronizationPoint;
1717
import net.lecousin.framework.io.IO.AbstractIO;
1818
import net.lecousin.framework.io.IO.Seekable.SeekType;
19-
import net.lecousin.framework.mutable.MutableInteger;
2019
import net.lecousin.framework.mutable.MutableLong;
2120
import net.lecousin.framework.util.Pair;
2221
import net.lecousin.framework.util.RunnableWithParameter;
@@ -800,32 +799,24 @@ protected long getSizeSync() throws IOException {
800799
}
801800

802801
protected AsyncWork<Long, IOException> getSizeAsync() {
802+
@SuppressWarnings("unchecked")
803+
AsyncWork<Long, IOException>[] sizes = new AsyncWork[ios.size()];
804+
for (int i = 0; i < ios.size(); ++i)
805+
sizes[i] = ((IO.KnownSize)ios.get(i)).getSizeAsync();
806+
JoinPoint<IOException> jp = JoinPoint.fromSynchronizationPointsSimilarError(sizes);
803807
AsyncWork<Long, IOException> result = new AsyncWork<Long, IOException>();
804-
MutableLong total = new MutableLong(0);
805-
MutableInteger index = new MutableInteger(0);
806-
((IO.KnownSize)ios.get(0)).getSizeAsync().listenInline(new AsyncWorkListener<Long, IOException>() {
807-
@Override
808-
public void ready(Long size) {
809-
total.add(size.longValue());
810-
if (index.inc() == ios.size())
811-
result.unblockSuccess(Long.valueOf(total.get()));
812-
else
813-
((IO.KnownSize)ios.get(index.get())).getSizeAsync().listenInline(this);
814-
}
815-
816-
@Override
817-
public void error(IOException error) {
818-
result.unblockError(error);
819-
}
820-
821-
@Override
822-
public void cancelled(CancelException event) {
823-
result.unblockCancel(event);
824-
}
825-
});
808+
jp.listenInline(
809+
() -> {
810+
long total = 0;
811+
for (int i = 0; i < sizes.length; ++i)
812+
total += sizes[i].getResult().longValue();
813+
result.unblockSuccess(Long.valueOf(total));
814+
},
815+
result
816+
);
826817
return result;
827818
}
828-
819+
829820

830821
protected long getPosition() {
831822
return pos;

0 commit comments

Comments
 (0)