-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move general util classes to their own types
- Loading branch information
Showing
4 changed files
with
112 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ns/barrage/src/main/java/io/deephaven/extensions/barrage/ConsecutiveDrainableStreams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending | ||
// | ||
package io.deephaven.extensions.barrage; | ||
|
||
import io.deephaven.UncheckedDeephavenException; | ||
import io.deephaven.extensions.barrage.util.DefensiveDrainable; | ||
import io.deephaven.util.datastructures.SizeException; | ||
import io.grpc.Drainable; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
public class ConsecutiveDrainableStreams extends DefensiveDrainable { | ||
final DefensiveDrainable[] streams; | ||
|
||
public ConsecutiveDrainableStreams(final @NotNull DefensiveDrainable... streams) { | ||
this.streams = streams; | ||
} | ||
|
||
@Override | ||
public int drainTo(final OutputStream outputStream) throws IOException { | ||
int total = 0; | ||
for (final DefensiveDrainable stream : streams) { | ||
final int expected = total + stream.available(); | ||
total += ((Drainable) stream).drainTo(outputStream); | ||
if (expected != total) { | ||
throw new IllegalStateException("drained message drained wrong number of bytes"); | ||
} | ||
if (total < 0) { | ||
throw new IllegalStateException("drained message is too large; exceeds Integer.MAX_VALUE"); | ||
} | ||
} | ||
return total; | ||
} | ||
|
||
@Override | ||
public int available() throws SizeException, IOException { | ||
int total = 0; | ||
for (final DefensiveDrainable stream : streams) { | ||
total += stream.available(); | ||
if (total < 0) { | ||
throw new SizeException("drained message is too large; exceeds Integer.MAX_VALUE", total); | ||
} | ||
} | ||
return total; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
for (final DefensiveDrainable stream : streams) { | ||
try { | ||
stream.close(); | ||
} catch (final IOException e) { | ||
throw new UncheckedDeephavenException("unexpected IOException", e); | ||
} | ||
} | ||
super.close(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../barrage/src/main/java/io/deephaven/extensions/barrage/DrainableByteArrayInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending | ||
// | ||
package io.deephaven.extensions.barrage; | ||
|
||
import io.deephaven.extensions.barrage.util.DefensiveDrainable; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Objects; | ||
|
||
public class DrainableByteArrayInputStream extends DefensiveDrainable { | ||
|
||
private byte[] buf; | ||
private final int offset; | ||
private final int length; | ||
|
||
public DrainableByteArrayInputStream(final byte[] buf, final int offset, final int length) { | ||
this.buf = Objects.requireNonNull(buf); | ||
this.offset = offset; | ||
this.length = length; | ||
} | ||
|
||
@Override | ||
public int available() { | ||
if (buf == null) { | ||
return 0; | ||
} | ||
return length; | ||
} | ||
|
||
@Override | ||
public int drainTo(final OutputStream outputStream) throws IOException { | ||
if (buf != null) { | ||
try { | ||
outputStream.write(buf, offset, length); | ||
} finally { | ||
buf = null; | ||
} | ||
return length; | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters