-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
347 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
13 changes: 13 additions & 0 deletions
13
lis-commons-io/src/main/java/com/link_intersystems/io/IOConsumer.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,13 @@ | ||
package com.link_intersystems.io; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* An io-related {@link java.util.function.Consumer} api that supports {@link IOException}s. | ||
* | ||
* @param <T> | ||
*/ | ||
public interface IOConsumer<T> { | ||
|
||
public void accept(T io) throws IOException; | ||
} |
55 changes: 55 additions & 0 deletions
55
lis-commons-io/src/main/java/com/link_intersystems/io/IOConsumers.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,55 @@ | ||
package com.link_intersystems.io; | ||
|
||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.Channels; | ||
import java.nio.channels.ReadableByteChannel; | ||
import java.nio.channels.WritableByteChannel; | ||
|
||
/** | ||
* Factory for creating {@link IOConsumer} adapters. | ||
*/ | ||
public class IOConsumers { | ||
|
||
/** | ||
* Creates an {@link IOConsumer<WritableByteChannel>} adapter for an {@link IOConsumer<OutputStream>}. | ||
* | ||
* @param outputStreamIOConsumer | ||
* @return | ||
*/ | ||
public static IOConsumer<WritableByteChannel> adaptOutputStream(IOConsumer<OutputStream> outputStreamIOConsumer) { | ||
return writer -> { | ||
try (OutputStream outputStream = Channels.newOutputStream(writer)) { | ||
outputStreamIOConsumer.accept(outputStream); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Creates an {@link IOConsumer<WritableByteChannel>} that will copy the given {@link ReadableByteChannel} to the {@link WritableByteChannel} when | ||
* {@link IOConsumer#accept(Object)} is invoked. A direct {@link ByteBuffer} of size 8192 is used. | ||
* | ||
* @param readableByteChannel | ||
*/ | ||
public static IOConsumer<WritableByteChannel> readableChannelCopyConsumer(ReadableByteChannel readableByteChannel) { | ||
return readableChannelCopyConsumer(readableByteChannel, ByteBuffer.allocateDirect(8192)); | ||
} | ||
|
||
/** | ||
* Creates an {@link IOConsumer<WritableByteChannel>} that will copy the given {@link ReadableByteChannel} to the {@link WritableByteChannel} when | ||
* {@link IOConsumer#accept(Object)} is invoked. The given {@link ByteBuffer} will be used for the copy process. | ||
* | ||
* @param readableByteChannel | ||
* @param byteBuffer the {@link ByteBuffer} to use for the copy process. | ||
*/ | ||
public static IOConsumer<WritableByteChannel> readableChannelCopyConsumer(ReadableByteChannel readableByteChannel, ByteBuffer byteBuffer) { | ||
return contentWriter -> { | ||
while (readableByteChannel.read(byteBuffer) != -1) { | ||
byteBuffer.flip(); | ||
contentWriter.write(byteBuffer); | ||
byteBuffer.flip(); | ||
} | ||
}; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
lis-commons-io/src/main/java/com/link_intersystems/io/StringInputStream.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,95 @@ | ||
package com.link_intersystems.io; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.nio.charset.Charset; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* An {@link InputStream} adapter for a {@link CharSequence}.Even though this class is based on the {@link CharSequence} interface, | ||
* the is named {@link StringInputStream}, because almost noone would find it if it would be named CharSequenceInputStream. | ||
*/ | ||
public class StringInputStream extends InputStream { | ||
private CharBuffer charBuffer = CharBuffer.allocate(1); | ||
private ByteBuffer byteBuffer = ByteBuffer.allocate(0); | ||
|
||
private int pos = 0; | ||
private CharSequence charSequence; | ||
|
||
private Charset charset; | ||
private int readLimitPos = -1; | ||
private int resetPos = -1; | ||
|
||
public StringInputStream(CharSequence charSequence) { | ||
this(charSequence, UTF_8); | ||
} | ||
|
||
public StringInputStream(CharSequence charSequence, Charset charset) { | ||
this.charSequence = requireNonNull(charSequence); | ||
this.charset = requireNonNull(charset); | ||
} | ||
|
||
@Override | ||
public boolean markSupported() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public synchronized void mark(int readlimit) { | ||
this.readLimitPos = pos + readlimit; | ||
this.resetPos = pos; | ||
} | ||
|
||
@Override | ||
public synchronized void reset() throws IOException { | ||
if (readLimitPos == -1) { | ||
throw new IOException("Stream not marked."); | ||
} | ||
|
||
if (pos > readLimitPos) { | ||
throw new IOException("Read limit exceeded."); | ||
} | ||
|
||
pos = resetPos; | ||
byteBuffer = ByteBuffer.allocate(0); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
try { | ||
if (!byteBuffer.hasRemaining()) { | ||
int charAt = readChar(); | ||
if (charAt == -1) { | ||
return -1; | ||
} | ||
|
||
charBuffer.put((char) charAt); | ||
charBuffer.flip(); | ||
byteBuffer = charset.encode(charBuffer); | ||
charBuffer.flip(); | ||
} | ||
|
||
return (int) byteBuffer.get(); | ||
} catch (NullPointerException e) { | ||
throw new IOException("Stream closed."); | ||
} | ||
} | ||
|
||
private int readChar() { | ||
if (pos < charSequence.length()) { | ||
return charSequence.charAt(pos++); | ||
} | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
charSequence = null; | ||
byteBuffer = null; | ||
charBuffer = null; | ||
} | ||
} |
Oops, something went wrong.