Skip to content

Instantly share code, notes, and snippets.

View ncruces's full-sized avatar

Nuno Cruces ncruces

View GitHub Profile
@ncruces
ncruces / AndroidBitmap.java
Last active March 14, 2019 14:54
JNA wrapper for Android NDK bitmap.h
package io.github.ncruces.utils;
import android.graphics.Bitmap;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.sun.jna.JNIEnv;
import com.sun.jna.Native;
@ncruces
ncruces / NativeBool.java
Last active May 17, 2022 15:10
Basic JNA types
package io.github.ncruces.utils;
import com.sun.jna.IntegerType;
import com.sun.jna.Native;
@SuppressWarnings("all")
public class NativeBool extends IntegerType {
private static final long serialVersionUID = 1L;
public static final int SIZE = Native.BOOL_SIZE;
@ncruces
ncruces / CachedParcelable.java
Created June 7, 2018 09:58
Caches a Parcelable (e.g. a Bitmap) in Parcel memory (i.e. off the heap)
package io.github.ncruces.utils;
import android.graphics.Bitmap;
import android.os.BadParcelableException;
import android.os.Parcel;
import android.os.Parcelable;
public final class CachedParcelable<T extends Parcelable> implements AutoCloseable {
private final Parcelable.Creator<T> creator;
private Parcel cache;
@ncruces
ncruces / Bundler.java
Last active July 2, 2021 09:48 — forked from f2prateek/Bundler.java
Fluent API for working with bundles
package io.github.ncruces.utils;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.util.Size;
import android.util.SizeF;
import android.util.SparseArray;
@ncruces
ncruces / BoundedInputStream.java
Last active May 21, 2018 02:01
InputStream that reads at most N bytes from underlying stream
package io.github.ncruces.utils;
import java.io.IOException;
import java.io.InputStream;
import static java.lang.Math.min;
public class BoundedInputStream extends InputStream {
private final InputStream in;
private final long max;
@ncruces
ncruces / ByteBufferChannel.java
Last active May 21, 2018 02:02
ByteBuffer backed SeekableByteChannel
package io.github.ncruces.utils;
import java.nio.ByteBuffer;
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.SeekableByteChannel;
import static java.lang.Math.min;
public final class ByteBufferChannel implements SeekableByteChannel {
private final ByteBuffer buf;
@ncruces
ncruces / ByteBufferInputStream.java
Last active May 21, 2018 02:03
InputStream that reads from a ByteBuffer
package io.github.ncruces.utils;
import java.io.InputStream;
import java.nio.ByteBuffer;
public final class ByteBufferInputStream extends InputStream {
private final ByteBuffer buf;
public ByteBufferInputStream(ByteBuffer buffer) {
if (buffer == null) throw new NullPointerException();
@ncruces
ncruces / CharArraySequence.java
Last active September 12, 2021 10:54
CharSequence backed by a char[]
package io.github.ncruces.utils;
public final class CharArraySequence implements CharSequence {
private final char[] buf;
private final int off, len;
public CharArraySequence(char[] value) {
buf = value;
off = 0;
len = value.length;
@ncruces
ncruces / SearchIterator.java
Last active June 7, 2018 10:40
Plain Java/Android StringSearch (ported from ICU; unfortunately the original had a few bugs at this point)
/*
*******************************************************************************
* Copyright (C) 1996-2000, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/SearchIterator.java,v $
* $Date: 2002/04/03 19:13:56 $
* $Revision: 1.6 $
*
@ncruces
ncruces / InterruptibleAsyncTaskLoader.java
Last active December 12, 2017 03:11
AsyncTaskLoader that can be interrupted by calling cancelLoadInBackground
package io.github.ncruces.utils;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.os.OperationCanceledException;
import static java.lang.Thread.currentThread;
import static java.lang.Thread.interrupted;
public abstract class InterruptibleAsyncTaskLoader<D> extends AsyncTaskLoader<D> {