Skip to content

Instantly share code, notes, and snippets.

@nyg
nyg / AllJCAServices.java
Last active April 2, 2024 11:09
List all JCA security provider services and export them to a CSV file.
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.Provider;
import java.security.Security;
import java.util.Arrays;
import java.util.Collection;
@nyg
nyg / iOSCreatePDF.swift
Last active April 2, 2024 11:09
iOS, Swift: Create a PDF file from an HTML string.
// Thanks to http://www.labs.saachitech.com/2012/10/23/pdf-generation-using-uiprintpagerenderer
// Note: including images in the HTML won't work, see here:
// https://github.com/nyg/HTMLWithImagesToPDF
import UIKit
// 1. Create a print formatter
let html = "<b>Hello <i>World!</i></b>"
let fmt = UIMarkupTextPrintFormatter(markupText: html)
@nyg
nyg / Uptime.swift
Last active April 1, 2024 21:35
Get boot time and uptime for macOS & iOS.
// https://stackoverflow.com/a/45068046/5536516
import Foundation
func kernelBootTime() -> timeval {
var mib = [ CTL_KERN, KERN_BOOTTIME ]
var bootTime = timeval()
var bootTimeSize = MemoryLayout<timeval>.size
@nyg
nyg / uptime.c
Last active April 1, 2024 21:33
Get boot time and uptime on macOS in C.
// Tested on macOS 10.12.
//
// Usage:
// $ clang uptime.c && ./a.out
// boot time (UNIX time): 1502299682.147510
// uptime (seconds): 410563.269028116
#include <stdlib.h>
#include <errno.h>
#include <string.h>
@nyg
nyg / div_euc_hex.c
Last active March 2, 2024 21:28
Euclidean division in C.
#include <stdio.h>
int main(int argc, const char** argv) {
unsigned int a = 0xDEADBEEF;
unsigned int b = 0xBABE;
unsigned int q = a / b;
unsigned int r = a % b;
printf("%u = %u * %u + %u\n", a, q, b, r);
@nyg
nyg / enlarge_image.php
Last active February 27, 2024 22:15
Enlarge a given image.
<?php
$filename = $argv[1];
$growthFactor = $argv[2];
list($width_orig, $height_orig) = getimagesize($filename);
$width = $width_orig * $growthFactor;
$height = $height_orig * $growthFactor;
@nyg
nyg / EXIFUserComment.swift
Last active January 22, 2024 13:41
Get and set an EXIF UserComment to a JPEG image using the ImageIO framework.
// Note: to add a JPEG COM marker go here:
// https://gist.github.com/nyg/bdeae8190a41b4b56bde8e13dd471ecc
import Foundation
import ImageIO
#if os(iOS)
import MobileCoreServices
#endif
@nyg
nyg / FreeSwap.java
Created January 20, 2024 10:22
Display available free Swap memory using JMX
package edu.self.nyg.example.jmx.app;
import java.lang.management.ManagementFactory;
import java.text.NumberFormat;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import lombok.extern.slf4j.Slf4j;
@nyg
nyg / use-local-storage.js
Created December 29, 2023 20:14
Custom useLocalStorage hook for Next.js
// Based on https://usehooks.com/useLocalStorage/, modified to be used with
// Next.js' server-side rendering components.
import { useState } from "react";
export default function useLocalStorage(key, initialValue) {
// There is no need to pass the inital value here as this will be executed on
// the server side, so window.localStorage is not available.
const [stateValue, setStateValue] = useState()
@nyg
nyg / request.mjs
Created December 29, 2023 19:42
HTTP request with Node.js
import http from 'http'
const options = {
hostname: 'perdu.com',
method: 'GET',
}
const req = http.request(options, res => {
console.log(`Status: ${res.statusCode}`)