-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
various stubs, more work towards httpclient
- Loading branch information
1 parent
286f52b
commit 356f26f
Showing
24 changed files
with
1,173 additions
and
28 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
2 changes: 1 addition & 1 deletion
2
...jvmdg/j11/impl/HttpClientBuilderImpl.java → .../j11/impl/http/HttpClientBuilderImpl.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
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
78 changes: 78 additions & 0 deletions
78
java-api/src/java11/java/xyz/wagyourtail/jvmdg/j11/impl/http/IterablePublisher.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,78 @@ | ||
package xyz.wagyourtail.jvmdg.j11.impl.http; | ||
|
||
import java.util.Iterator; | ||
import java.util.concurrent.Flow; | ||
|
||
public class IterablePublisher<T> implements Flow.Publisher<T> { | ||
private final Iterable<T> iterable; | ||
private final Throwable throwable; | ||
|
||
public IterablePublisher(Iterable<T> iterable, Throwable throwable) { | ||
this.iterable = iterable; | ||
this.throwable = throwable; | ||
} | ||
|
||
public IterablePublisher(Iterable<T> iterable) { | ||
this(iterable, null); | ||
} | ||
|
||
@Override | ||
public void subscribe(Flow.Subscriber<? super T> subscriber) { | ||
subscriber.onSubscribe(new Subscription(subscriber)); | ||
} | ||
|
||
private class Subscription implements Flow.Subscription { | ||
private final Flow.Subscriber<? super T> subscriber; | ||
private final Iterator<T> iterator; | ||
private volatile boolean completed; | ||
private volatile boolean cancelled; | ||
private volatile Throwable error; | ||
|
||
public Subscription(Flow.Subscriber<? super T> subscriber) { | ||
this.subscriber = subscriber; | ||
this.iterator = iterable.iterator(); | ||
this.error = throwable; | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
if (n <= 0) { | ||
subscriber.onError(new IllegalArgumentException("n <= 0")); | ||
return; | ||
} | ||
if (completed || cancelled) { | ||
return; | ||
} | ||
if (error != null) { | ||
completed = true; | ||
subscriber.onError(error); | ||
return; | ||
} | ||
try { | ||
synchronized (this) { | ||
for (long i = 0; i < n; i++) { | ||
if (cancelled) { | ||
break; | ||
} | ||
if (iterator.hasNext()) { | ||
subscriber.onNext(iterator.next()); | ||
} else { | ||
completed = true; | ||
subscriber.onComplete(); | ||
break; | ||
} | ||
} | ||
} | ||
} catch (Throwable t) { | ||
error = t; | ||
subscriber.onError(t); | ||
completed = true; | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
cancelled = true; | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
java-api/src/java11/java/xyz/wagyourtail/jvmdg/j11/impl/http/StreamIterator.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 xyz.wagyourtail.jvmdg.j11.impl.http; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Supplier; | ||
|
||
public class StreamIterator implements Iterator<ByteBuffer> { | ||
public static final int BUFSIZE = Integer.parseInt(System.getProperty("jdk.httpclient.bufsize", "16384")); | ||
|
||
final InputStream is; | ||
final Supplier<? extends ByteBuffer> bufSupplier; | ||
private volatile boolean eof; | ||
volatile ByteBuffer nextBuffer; | ||
volatile boolean need2Read = true; | ||
volatile boolean haveNext; | ||
|
||
public StreamIterator(InputStream is) { | ||
this(is, () -> ByteBuffer.allocate(BUFSIZE)); | ||
} | ||
|
||
StreamIterator(InputStream is, Supplier<? extends ByteBuffer> bufSupplier) { | ||
this.is = is; | ||
this.bufSupplier = bufSupplier; | ||
} | ||
|
||
// Throwable error() { | ||
// return error; | ||
// } | ||
|
||
private int read() throws IOException { | ||
if (eof) | ||
return -1; | ||
nextBuffer = bufSupplier.get(); | ||
nextBuffer.clear(); | ||
byte[] buf = nextBuffer.array(); | ||
int offset = nextBuffer.arrayOffset(); | ||
int cap = nextBuffer.capacity(); | ||
int n = is.read(buf, offset, cap); | ||
if (n == -1) { | ||
eof = true; | ||
return -1; | ||
} | ||
//flip | ||
nextBuffer.limit(n); | ||
nextBuffer.position(0); | ||
return n; | ||
} | ||
|
||
/** | ||
* Close stream in this instance. | ||
* UncheckedIOException may be thrown if IOE happens at InputStream::close. | ||
*/ | ||
private void closeStream() { | ||
try { | ||
is.close(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized boolean hasNext() { | ||
if (need2Read) { | ||
try { | ||
haveNext = read() != -1; | ||
if (haveNext) { | ||
need2Read = false; | ||
} | ||
} catch (IOException e) { | ||
haveNext = false; | ||
need2Read = false; | ||
throw new UncheckedIOException(e); | ||
} finally { | ||
if (!haveNext) { | ||
closeStream(); | ||
} | ||
} | ||
} | ||
return haveNext; | ||
} | ||
|
||
@Override | ||
public synchronized ByteBuffer next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
need2Read = true; | ||
return nextBuffer; | ||
} | ||
|
||
} |
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
7 changes: 7 additions & 0 deletions
7
java-api/src/java11/java/xyz/wagyourtail/jvmdg/j11/stub/java_net_http/J_N_H_HttpHeaders.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,7 @@ | ||
package xyz.wagyourtail.jvmdg.j11.stub.java_net_http; | ||
|
||
import xyz.wagyourtail.jvmdg.version.Adapter; | ||
|
||
@Adapter("Ljava/net/http/HttpHeaders;") | ||
public class J_N_H_HttpHeaders { | ||
} |
Oops, something went wrong.