Skip to content

Instantly share code, notes, and snippets.

View ncruces's full-sized avatar

Nuno Cruces ncruces

  • Lisboa, Portugal
View GitHub Profile
@ncruces
ncruces / TupleConverter.cs
Last active May 9, 2017 01:40
Converts a Tuple to and from a JSON array
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Reflection;
namespace Newtonsoft.Json.Converters
{
/// <summary>
/// Converts a <see cref="Tuple"/> to and from a JSON array.
/// </summary>
@ncruces
ncruces / InvariantUrlParameterFormatter.cs
Last active November 27, 2019 17:50
Culture invariant IUrlParameterFormatter for https://github.com/reactiveui
using System;
using System.Globalization;
using System.Reflection;
namespace Refit
{
public sealed class InvariantUrlParameterFormatter : IUrlParameterFormatter
{
public string Format(object parameterValue, ParameterInfo parameterInfo)
{
@ncruces
ncruces / CharSequenceCharacterIterator.java
Last active December 12, 2017 03:01
CharacterIterator backed by a CharSequence
package io.github.ncruces.utils;
import java.text.CharacterIterator;
public final class CharSequenceCharacterIterator implements CharacterIterator {
private final CharSequence seq;
private int pos;
public CharSequenceCharacterIterator(CharSequence sequence) {
if (sequence == null) throw new NullPointerException();
@ncruces
ncruces / ByteBufferOutputStream.java
Last active July 2, 2021 17:54
OutputStream that writes to a direct ByteBuffer
package io.github.ncruces.utils;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import static java.lang.Math.max;
public final class ByteBufferOutputStream extends OutputStream {
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private ByteBuffer buf;
@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> {
@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 / 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 / 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 / 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 / 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;