All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gnome.soup.MultipartInputStream Maven / Gradle / Ivy

The newest version!
// Java-GI - Java language bindings for GObject-Introspection-based libraries
// Copyright (C) 2022-2024 Jan-Willem Harmannij
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see .
//
// This file has been generated with Java-GI.
// Do not edit this file directly!
// Visit  for more information.
//
package org.gnome.soup;

import io.github.jwharm.javagi.base.GErrorException;
import io.github.jwharm.javagi.base.UnsupportedPlatformException;
import io.github.jwharm.javagi.gobject.InstanceCache;
import io.github.jwharm.javagi.interop.ArenaCloseAction;
import io.github.jwharm.javagi.interop.Interop;
import io.github.jwharm.javagi.interop.Platform;
import java.lang.Deprecated;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import javax.annotation.processing.Generated;
import org.gnome.gio.AsyncReadyCallback;
import org.gnome.gio.AsyncResult;
import org.gnome.gio.Cancellable;
import org.gnome.gio.FilterInputStream;
import org.gnome.gio.InputStream;
import org.gnome.gio.PollableInputStream;
import org.gnome.glib.Type;
import org.gnome.gobject.GObject;
import org.gnome.gobject.Value;
import org.jetbrains.annotations.Nullable;

/**
 * Handles streams of multipart messages.
 * 

* This adds support for the multipart responses. For handling the * multiple parts the user needs to wrap the {@link org.gnome.gio.InputStream} obtained by * sending the request with a {@link MultipartInputStream} and use * {@link MultipartInputStream#nextPart} before reading. Responses * which are not wrapped will be treated like non-multipart responses. *

* Note that although {@code SoupMultipartInputStream} is a {@link org.gnome.gio.InputStream}, * you should not read directly from it, and the results are undefined * if you do. */ @Generated("io.github.jwharm.JavaGI") public final class MultipartInputStream extends FilterInputStream implements PollableInputStream { static { Soup.javagi$ensureInitialized(); } /** * Create a MultipartInputStream proxy instance for the provided memory address. * * @param address the memory address of the native object */ public MultipartInputStream(MemorySegment address) { super(Interop.reinterpret(address, getMemoryLayout().byteSize())); } /** * Creates a new {@code SoupMultipartInputStream} that wraps the * {@link org.gnome.gio.InputStream} obtained by sending the {@link Message}. *

* Reads should not be done directly through this object, use the input streams * returned by {@link MultipartInputStream#nextPart} or its async * counterpart instead. * @param msg the {@code SoupMessage} the response is related to. * @param baseStream the {@code GInputStream} returned by sending the request. * @throws UnsupportedPlatformException when run on a platform other than linux */ public MultipartInputStream(Message msg, InputStream baseStream) throws UnsupportedPlatformException { super(constructNew(msg, baseStream)); InstanceCache.put(handle(), this); } /** * Get the GType of the MultipartInputStream class * * @return the GType */ public static Type getType() { return Interop.getType("soup_multipart_input_stream_get_type"); } /** * Returns this instance as if it were its parent type. This is mostly * synonymous to the Java {@code super} keyword, but will set the native * typeclass function pointers to the parent type. When overriding a native * virtual method in Java, "chaining up" with {@code super.methodName()} * doesn't work, because it invokes the overridden function pointer again. * To chain up, call {@code asParent().methodName()}. This will call the * native function pointer of this virtual method in the typeclass of the * parent type. */ protected MultipartInputStream asParent() { MultipartInputStream _parent = new MultipartInputStream(handle()); _parent.callParent(true); return _parent; } private static MemorySegment constructNew(Message msg, InputStream baseStream) { Platform.checkSupportedPlatform("linux"); MemorySegment _result; try { _result = (MemorySegment) MethodHandles.soup_multipart_input_stream_new.invokeExact( (MemorySegment) (msg == null ? MemorySegment.NULL : msg.handle()), (MemorySegment) (baseStream == null ? MemorySegment.NULL : baseStream.handle())); } catch (Throwable _err) { throw new AssertionError(_err); } return _result; } /** * Obtains the headers for the part currently being processed. *

* Note that the {@code MessageHeaders} that are returned are owned by the * {@code SoupMultipartInputStream} and will be replaced when a call is made to * {@link MultipartInputStream#nextPart} or its async counterpart, so if * keeping the headers is required, a copy must be made. *

* Note that if a part had no headers at all an empty {@code MessageHeaders} * will be returned. * @return a {@code SoupMessageHeaders} * containing the headers for the part currently being processed or * {@code null} if the headers failed to parse. */ public MessageHeaders getHeaders() { MemorySegment _result; try { _result = (MemorySegment) MethodHandles.soup_multipart_input_stream_get_headers.invokeExact( handle()); } catch (Throwable _err) { throw new AssertionError(_err); } return MemorySegment.NULL.equals(_result) ? null : new MessageHeaders(_result); } /** * Obtains an input stream for the next part. *

* When dealing with a multipart response the input stream needs to be wrapped * in a {@code SoupMultipartInputStream} and this function or its async counterpart * need to be called to obtain the first part for reading. *

* After calling this function, * {@link MultipartInputStream#getHeaders} can be used to obtain the * headers for the first part. A read of 0 bytes indicates the end of * the part; a new call to this function should be done at that point, * to obtain the next part. * @param cancellable a {@code GCancellable} * @return a new {@code GInputStream}, or * {@code null} if there are no more parts * @throws GErrorException see {@link org.gnome.glib.GError} */ public InputStream nextPart(@Nullable Cancellable cancellable) throws GErrorException { try (var _arena = Arena.ofConfined()) { MemorySegment _gerror = _arena.allocate(ValueLayout.ADDRESS); MemorySegment _result; try { _result = (MemorySegment) MethodHandles.soup_multipart_input_stream_next_part.invokeExact( handle(), (MemorySegment) (cancellable == null ? MemorySegment.NULL : cancellable.handle()), _gerror); } catch (Throwable _err) { throw new AssertionError(_err); } if (GErrorException.isErrorSet(_gerror)) { throw new GErrorException(_gerror); } return (InputStream) InstanceCache.getForType(_result, InputStream.InputStreamImpl::new, true); } } /** * Obtains a {@link org.gnome.gio.InputStream} for the next request. *

* See {@link MultipartInputStream#nextPart} for details on the workflow. * @param ioPriority the I/O priority for the request. * @param cancellable a {@code GCancellable}. * @param callback callback to call when request is satisfied. */ public void nextPartAsync(int ioPriority, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) { try (var _arena = Arena.ofConfined()) { final Arena _callbackScope = Arena.ofConfined(); if (callback != null) ArenaCloseAction.CLEANER.register(callback, new ArenaCloseAction(_callbackScope)); try { MethodHandles.soup_multipart_input_stream_next_part_async.invokeExact(handle(), ioPriority, (MemorySegment) (cancellable == null ? MemorySegment.NULL : cancellable.handle()), (MemorySegment) (callback == null ? MemorySegment.NULL : callback.toCallback(_callbackScope)), MemorySegment.NULL); } catch (Throwable _err) { throw new AssertionError(_err); } } } /** * Finishes an asynchronous request for the next part. * @param result a {@code GAsyncResult}. * @return a newly created * {@link org.gnome.gio.InputStream} for reading the next part or {@code null} if there are no * more parts. * @throws GErrorException see {@link org.gnome.glib.GError} */ public InputStream nextPartFinish(AsyncResult result) throws GErrorException { try (var _arena = Arena.ofConfined()) { MemorySegment _gerror = _arena.allocate(ValueLayout.ADDRESS); MemorySegment _result; try { _result = (MemorySegment) MethodHandles.soup_multipart_input_stream_next_part_finish.invokeExact( handle(), (MemorySegment) (result == null ? MemorySegment.NULL : result.handle()), _gerror); } catch (Throwable _err) { throw new AssertionError(_err); } if (GErrorException.isErrorSet(_gerror)) { throw new GErrorException(_gerror); } return (InputStream) InstanceCache.getForType(_result, InputStream.InputStreamImpl::new, true); } } /** * A {@link Builder} object constructs a {@code MultipartInputStream} * with the specified properties. * Use the various {@code set...()} methods to set properties, * and finish construction with {@link Builder#build()}. */ public static Builder builder() { return new Builder<>(); } public static class MultipartInputStreamClass extends FilterInputStream.FilterInputStreamClass { /** * Create a MultipartInputStreamClass proxy instance for the provided memory address. * * @param address the memory address of the native object */ public MultipartInputStreamClass(MemorySegment address) { super(Interop.reinterpret(address, getMemoryLayout().byteSize())); } /** * Allocate a new MultipartInputStreamClass. * * @param arena to control the memory allocation scope */ public MultipartInputStreamClass(Arena arena) { super(arena.allocate(getMemoryLayout())); } /** * Allocate a new MultipartInputStreamClass. * The memory is allocated with {@link Arena#ofAuto}. */ public MultipartInputStreamClass() { super(Arena.ofAuto().allocate(getMemoryLayout())); } /** * The memory layout of the native struct. * @return the memory layout */ public static MemoryLayout getMemoryLayout() { return MemoryLayout.structLayout( FilterInputStream.FilterInputStreamClass.getMemoryLayout().withName("parent_class") ).withName("SoupMultipartInputStreamClass"); } /** * Allocate a new MultipartInputStreamClass. * * @param arena to control the memory allocation scope * @return a new, uninitialized {@link MultipartInputStreamClass} * @deprecated Replaced by {@link MultipartInputStreamClass#MultipartInputStreamClass(Arena)} */ @Deprecated public static MultipartInputStreamClass allocate(Arena arena) { MemorySegment segment = arena.allocate(getMemoryLayout()); return new MultipartInputStreamClass(segment); } /** * Read the value of the field {@code parent_class}. * * @return The value of the field {@code parent_class} */ public FilterInputStream.FilterInputStreamClass readParentClass() { long _offset = getMemoryLayout().byteOffset(MemoryLayout.PathElement.groupElement("parent_class")); MemorySegment _slice = handle().asSlice(_offset, FilterInputStream.FilterInputStreamClass.getMemoryLayout()); return new FilterInputStream.FilterInputStreamClass(handle().asSlice(_offset)); } /** * Write a value in the field {@code parent_class}. * * @param parentClass The new value for the field {@code parent_class} */ public void writeParentClass(FilterInputStream.FilterInputStreamClass parentClass) { long _offset = getMemoryLayout().byteOffset(MemoryLayout.PathElement.groupElement("parent_class")); MemorySegment _slice = handle().asSlice(_offset, FilterInputStream.FilterInputStreamClass.getMemoryLayout()); if (parentClass == null) { _slice.fill((byte) 0); } else { _slice.copyFrom(parentClass.handle()); } } } /** * Inner class implementing a builder pattern to construct a GObject with * properties. * * @param the type of the Builder that is returned */ public static class Builder> extends FilterInputStream.Builder { /** * Default constructor for a {@code Builder} object. */ protected Builder() { } /** * Finish building the {@code MultipartInputStream} object. This will call * {@link GObject#withProperties} to create a new GObject instance, * which is then cast to {@code MultipartInputStream}. * * @return a new instance of {@code MultipartInputStream} with the properties * that were set in the Builder object. * @throws UnsupportedPlatformException when run on an unsupported platform */ public MultipartInputStream build() throws UnsupportedPlatformException { try { Platform.checkSupportedPlatform("linux"); return (MultipartInputStream) GObject.withProperties(MultipartInputStream.getType(), getNames(), getValues()); } finally { for (Value _value : getValues()) _value.unset(); getArena().close(); } } /** * The {@link Message}. * @param message the value for the {@code message} property * @return the {@code Builder} instance is returned, to allow method chaining */ public B setMessage(Message message) { Arena _arena = getArena(); Value _value = new Value(_arena); _value.init(Message.getType()); _value.setObject((GObject) message); addBuilderProperty("message", _value); return (B) this; } } private static final class MethodHandles { static final MethodHandle soup_multipart_input_stream_new = Interop.downcallHandle( "soup_multipart_input_stream_new", FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS), false); static final MethodHandle soup_multipart_input_stream_get_headers = Interop.downcallHandle( "soup_multipart_input_stream_get_headers", FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS), false); static final MethodHandle soup_multipart_input_stream_next_part = Interop.downcallHandle( "soup_multipart_input_stream_next_part", FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS), false); static final MethodHandle soup_multipart_input_stream_next_part_async = Interop.downcallHandle( "soup_multipart_input_stream_next_part_async", FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS), false); static final MethodHandle soup_multipart_input_stream_next_part_finish = Interop.downcallHandle( "soup_multipart_input_stream_next_part_finish", FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS), false); } }