Skip to content

Instantly share code, notes, and snippets.

Avatar

Klaus Brunner klausbrunner

View GitHub Profile
@klausbrunner
klausbrunner / SimpleFuture.java
Created November 19, 2012 11:34
A very simple implementation of the Java Future interface, for passing a single value to some waiting thread. Uses a CountdownLatch to ensure thread safety and provide blocking-with-timeout functionality required by Future. Cancellation isn't supported.
View SimpleFuture.java
public final class ResultFuture implements Future<Result> {
private final CountDownLatch latch = new CountDownLatch(1);
private Result value;
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
@klausbrunner
klausbrunner / unsigned-java.md
Last active March 11, 2023 09:29
Dealing with unsigned integers in Java
View unsigned-java.md

Since version 1.0, Java's type system has remained pretty much the same when it comes to primitive types: we have signed short (16 bits), int (32 bits), and long (64 bits), but no unsigned counterparts to these.

This is rarely a problem, but sometimes it might seem to be: especially when reading and writing binary data formats that use unsigned values.

ByteBuffer buf = ...
int unsignedIntValue = buf.getInt(); 

Whenever the unsigned value is greater than Integer.MAX_VALUE, our variable unsignedIntValue will look like a negative number - since Java uses the common approach of representing signed values in two's complement encoding. But as long as we don't have to know the actual value, we don't have to care. Java doesn't change the bits if we don't ask it to, and writing out the number again will result in the exact same bytes as our previous input (if we make sure to use the same byte order, of course):

View create-test-zips.sh
# quickly create large/very populated zip files for testing purposes, without taking up lots of disk space
dd if=/dev/zero bs=1m count=10k | zip -9 single-10g-file.zip -
dd if=/dev/urandom bs=1 count=20000 | split -b 1 -a 4 - test_ && zip -q -m 20k-tiny-files.zip test_*
# this ensures CD entries with the LFH offset in the ZIP64 EIEF:
dd if=/dev/zero bs=1g count=6 | split -b 2g - test_ && zip -0 -m few-huge-files.zip test_*
View usno-sunrise-table-parser.py
#!/usr/bin/env python3
"""Parse sunrise/sunset tables as provided by the USNO website at https://aa.usno.navy.mil/data/RS_OneYear"""
import re
import argparse
def parse_table(filename: str):
def to_decimal_deg(latlon_tup) -> float:
assert len(latlon_tup) == 3 and re.match("[NESW]", latlon_tup[0])
@klausbrunner
klausbrunner / markdown_to_pdf_sans.sh
Last active July 8, 2022 14:13
Convert markdown with mermaid to PDF using sans fonts (tested on MacOS; pandoc and mactex installed via homebrew, mermaid-filter installed via npm)
View markdown_to_pdf_sans.sh
pandoc -t pdf -F mermaid-filter -o README.pdf README.md --pdf-engine=xelatex -V mainfont="PT Sans" -V monofont="PT Mono" -V papersize:a4
@klausbrunner
klausbrunner / rspamd-podman.md
Last active May 19, 2022 07:10
Running Rspamd with redis and unbound in Podman
View rspamd-podman.md
@klausbrunner
klausbrunner / proc.py
Created April 5, 2022 14:09
Given a sequence of pairs (A, B), return a list of 3-tuples (A, B, T) where T is a boolean that marks whether the sequence contains the inverted pair (B, A) as well.
View proc.py
from typing import Sequence
def mark_bidirectionals(pairs: Sequence) -> list:
"""Given a sequence of pairs (A, B), return a list of 3-tuples (A, B, T) where T is a boolean that
marks whether the sequence contains the inverted pair (B, A) as well. Order is preserved, but all
identical and inverted pairs to the right of (i.e. on higher indexes than) the original pair are removed.
Implementation note: pairs must be immutable (usable as dict keys)."""
marks = dict()
for a, b in pairs:
if (b, a) in marks:
@klausbrunner
klausbrunner / JacksonStreamingBindingTest.java
Last active March 14, 2021 05:14
Incrementally binding JSON objects in an array (list) using Jackson.
View JacksonStreamingBindingTest.java
package tv.xrm.test;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
public class JacksonStreamingBindingTest {
@klausbrunner
klausbrunner / counters.cql
Created November 20, 2012 14:25
creating and using a Cassandra counter column in CQL
View counters.cql
-- creating and using a Cassandra counter column in CQL
-- NOTE: this is CQL 2.0 syntax, it will NOT work with CQL 3 (for syntax reasons and because CQL 3.0 drops the very notion of dynamic columns...sigh)
CREATE KEYSPACE test WITH strategy_class = 'SimpleStrategy'
AND strategy_options:replication_factor = '1';
USE test;
CREATE TABLE stats (KEY text PRIMARY KEY) WITH comparator=text AND default_validation=counter;
@klausbrunner
klausbrunner / sshd_config
Last active April 13, 2020 13:42
Minimal, secure sshd_config (OpenSSH 8.2)
View sshd_config
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_rsa_key
ChallengeResponseAuthentication no
UsePAM yes
# Allow client to pass locale environment variables