jaulib v1.3.0
Jau Support Library (C++, Java, ..)
ByteInStream_URL.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022-2023 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.io;
25
26import java.nio.ByteBuffer;
27
28/**
29 * Ringbuffer-Based byte input stream with a URL connection provisioned data feed.
30 *
31 * Instance uses the native C++ object `jau::io::ByteInStream_URL`.
32 *
33 * Standard implementation uses [curl](https://curl.se/),
34 * hence all [*libcurl* network protocols](https://curl.se/docs/url-syntax.html) are supported,
35 * jau::io::uri::supported_protocols().
36 */
37public final class ByteInStream_URL implements ByteInStream {
38 private volatile long nativeInstance;
39 /* pp */ long getNativeInstance() { return nativeInstance; }
40
41 /**
42 * Construct a ringbuffer backed Http byte input stream
43 * @param url the URL of the data to read
44 * @param timeout maximum duration in milliseconds to wait @ check_available() for next bytes, where zero waits infinitely
45 */
46 public ByteInStream_URL(final String url, final long timeoutMS) {
47 try {
48 nativeInstance = ctorImpl(url, timeoutMS);
49 } catch (final Throwable t) {
50 System.err.println("ByteInStream_URL.ctor: native ctor failed: "+t.getMessage());
51 throw t;
52 }
53 }
54 private native long ctorImpl(final String url, final long timeoutMS);
55
56 @Override
57 public native void closeStream();
58
59 @Override
60 public void close() {
61 final long handle;
62 synchronized( this ) {
63 handle = nativeInstance;
64 nativeInstance = 0;
65 }
66 if( 0 != handle ) {
67 dtorImpl(handle);
68 }
69 }
70 private static native void dtorImpl(final long nativeInstance);
71
72 @Override
73 public void finalize() {
74 close();
75 }
76
77 @Override
78 public native boolean is_open();
79
80 @Override
81 public void clear(final IOState state) {
82 clearImpl( state.mask );
83 }
84 private native void clearImpl(int s);
85
86 @Override
87 public IOState rdState() {
88 return new IOState( rdStateImpl() );
89 }
90 private native int rdStateImpl();
91
92 @Override
93 public void setState(final IOState state) {
94 setStateImpl( state.mask );
95 }
96 private native void setStateImpl(int s);
97
98 @Override
99 public native boolean good();
100
101 @Override
102 public native boolean eof();
103
104 @Override
105 public native boolean fail();
106
107 @Override
108 public native boolean bad();
109
110 @Override
111 public native boolean timeout();
112
113 @Override
114 public native boolean available(final long n);
115
116 @Override
117 public native int read(final byte[] out, final int offset, final int length);
118
119 @Override
120 public int read(final ByteBuffer out) {
121 if( !Buffers.isDirect(out) ) {
122 throw new IllegalArgumentException("out buffer not direct");
123 }
124 final int res = read2Impl(out, (int)Buffers.getDirectBufferByteOffset(out));
125 out.limit(out.position() + res);
126 return res;
127 }
128 private native int read2Impl(Object out, int out_offset);
129
130 @Override
131 public native int peek(byte[] out, final int offset, final int length, final long peek_offset);
132
133 @Override
134 public native String id();
135
136 @Override
137 public native long discard_next(long N);
138
139 @Override
140 public native long tellg();
141
142 @Override
143 public native boolean has_content_size();
144
145 @Override
146 public native long content_size();
147
148 @Override
149 public native String toString();
150}
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:43
static boolean isDirect(final Object buf)
Helper routine to tell whether a buffer is direct or not.
Definition: Buffers.java:78
static long getDirectBufferByteOffset(final Object buf)
Helper routine to get the Buffer byte offset by taking into account the Buffer position and the under...
Definition: Buffers.java:96
Ringbuffer-Based byte input stream with a URL connection provisioned data feed.
native String toString()
int read(final ByteBuffer out)
Read from the source.
native long discard_next(long N)
Discard the next N bytes of the data.
native boolean eof()
Checks if end-of-file has been reached.
native boolean fail()
Checks if an error has occurred.
native int read(final byte[] out, final int offset, final int length)
Read from the source.
native boolean is_open()
Checks if the stream has an associated file.
void setState(final IOState state)
Sets state flags, by keeping its previous bits.
native boolean timeout()
Checks if a timeout (non-recoverable) has occurred.
native long tellg()
Returns the input position indicator, similar to std::basic_istream.
void close()
Close the stream if supported by the underlying mechanism and dispose the native instance.
native void closeStream()
Close the stream if supported by the underlying mechanism.
void clear(final IOState state)
Clears state flags by assignment to the given value.
native boolean good()
Checks if no error nor eof() has occurred i.e.
native boolean available(final long n)
Return whether n bytes are available in the input stream, if has_content_size() or using an asynchron...
native boolean has_content_size()
Returns true if implementation is aware of content_size(), otherwise false.
native int peek(byte[] out, final int offset, final int length, final long peek_offset)
Read from the source but do not modify the internal offset.
native long content_size()
Returns the content_size if known.
native String id()
return the id of this data source
IOState rdState()
Returns the current state flags.
ByteInStream_URL(final String url, final long timeoutMS)
Construct a ringbuffer backed Http byte input stream.
native boolean bad()
Checks if a non-recoverable error has occurred.
Mimic std::ios_base::iostate for state functionality, see iostate_func.
Definition: IOState.java:33
Abstract byte input stream object.