Skip to content

Instantly share code, notes, and snippets.

@lukhnos
lukhnos / ReferenceQueueLeaks.java
Created December 9, 2015 17:37
ReferenceQueue leaks demo
/**
* This demonstrates the leaks in ReferenceQueue.
*
* To run the demo, compile the source with:
*
* j2objc ReferenceQueueLeaks.java
* j2objcc ReferenceQueueLeaks.m
*
* Then use the Leaks tool in Instruments and have it run "a.out ReferenceQueueLeaks".
*/
@lukhnos
lukhnos / FastEnumGotcha3.java
Created September 9, 2015 14:56
A case where you can't fast-enumerate an Iterable, 3/3
// Solution 2: Use OCNI.
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class FastEnumGotcha {
static class Wrapper<T> {
T value;
@lukhnos
lukhnos / FastEnumGotcha2.java
Created September 9, 2015 14:56
A case where you can't fast-enumerate an Iterable, 2/3
// Solution 1: Use annotation.
import com.google.j2objc.annotations.LoopTranslation;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class FastEnumGotcha {
static class Wrapper<T> {
T value;
@lukhnos
lukhnos / FastEnumGotcha1.java
Created September 9, 2015 14:55
A case where you can't fast-enumerate an Iterable, 1/3
// This works perfectly as a Java program, but produces wrong output when
// translated into Objective-C. Can you spot why?
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class FastEnumGotcha {
static class Wrapper<T> {
T value;
@lukhnos
lukhnos / NIOLeaks.java
Last active September 4, 2015 15:34
Demonstrates that FileChannelImpl and FileChannel leak memory in j2objc, see https://github.com/google/j2objc/issues/603
import com.google.j2objc.annotations.AutoreleasePool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class NIOLeaks {
public static void main(String args[]) {
@lukhnos
lukhnos / ThrowableLeaks.java
Last active September 4, 2015 15:34
Demonstrates that Throwable leaks memory in j2objc, see https://github.com/google/j2objc/issues/601
import com.google.j2objc.annotations.AutoreleasePool;
public class ThrowableLeaks {
public static void main(String args[]) {
for (int i = 0; ; i++) {
foo(i);
bar(i);
try {
Thread.sleep(1000);
} catch (Exception e) {
@lukhnos
lukhnos / build.gradle
Created July 10, 2015 06:03
A typical Java project gradle setup that combines META-INF/services in the "super JAR"
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
// ...
}
@lukhnos
lukhnos / PreadBug.java
Last active August 29, 2015 14:23
libcore.io.Posix.preadBytes bug reproducible sample
// To reproduce the bug:
//
// j2objc PreadBug.java
// j2objcc PreadBug.m
// ./a.out PreadBug
//
// You'll see the following exception:
//
// java.io.IOException: pread failed: EBADF (Bad file descriptor)
@lukhnos
lukhnos / otpbox.py
Created February 6, 2015 08:24
A simple command line pyotp wrapper
import argparse
import json
import pyotp
def main():
parser = argparse.ArgumentParser()
parser.add_argument('json', nargs=1)
parser.add_argument('key', nargs=1)
@lukhnos
lukhnos / whoosh-cjk-analyser.md
Created February 4, 2014 09:12
How to Use Whoosh to Index Documents that Contain CJK Characters (First Take)

Whoosh's default analyzer does not handle CJK characters (in particular Chinese and Japanese) well. If you pass typical Chinese or Japanese paragraphes, often you'll find an entire sentence is treated as one token.

A Whoosh analyzer is consists of one tokenizer and zero or more filters. As a result, we can easily use this recipe from Lucene's CJKAnalyzer:

An Analyzer that tokenizes text with StandardTokenizer, normalizes content with CJKWidthFilter, folds case with LowerCaseFilter, forms bigrams of CJK with CJKBigramFilter, and filters stopwords with StopFilter

Which inspired me to make this first take:

class CJKFilter(Filter):
    def __call__(self, tokens):