Skip to content

Instantly share code, notes, and snippets.

@Crydust
Crydust / LoggingMap
Last active Feb 25, 2021
a logging map which can replace a HashMap and will log when it is being accessed or altered
View LoggingMap
import static java.util.stream.Collectors.toUnmodifiableSet;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
@Crydust
Crydust / ClientApp.java
Created Jan 24, 2021
Java socket server and client
View ClientApp.java
package spike;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Timer;
import static java.nio.charset.StandardCharsets.UTF_8;
@Crydust
Crydust / regions-fr.csv
Last active Jun 8, 2020
The regions and departments of France including their INSEE number and ISO 3166-2 code. (aka Liste des Départements et Régions en France)
View regions-fr.csv
Departement INSEE Region INSEE Departement ISO Region ISO Departement Region
01 84 FR-01 FR-ARA Ain Auvergne-Rhône-Alpes
02 32 FR-02 FR-HDF Aisne Hauts-de-France
03 84 FR-03 FR-ARA Allier Auvergne-Rhône-Alpes
04 93 FR-04 FR-PAC Alpes-de-Haute-Provence Provence-Alpes-Côte d'Azur
05 93 FR-05 FR-PAC Hautes-Alpes Provence-Alpes-Côte d'Azur
06 93 FR-06 FR-PAC Alpes-Maritimes Provence-Alpes-Côte d'Azur
07 84 FR-07 FR-ARA Ardèche Auvergne-Rhône-Alpes
08 44 FR-08 FR-GES Ardennes Grand Est
09 76 FR-09 FR-OCC Ariège Occitanie
@Crydust
Crydust / NaiveMavenVersion.java
Created Jan 30, 2019
NaiveMavenVersion Comparable
View NaiveMavenVersion.java
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Only these three version shemes are supported:
* <ul>
* <li>major[.minor[.incremental]][-buildnumber]</li>
* <li>major[.minor[.incremental]][-qualifier]</li>
* <li>qualifier</li>
@Crydust
Crydust / App.java
Last active Apr 2, 2021
HTTPServer post-process filter (written as answer to https://stackoverflow.com/q/14592299/11451)
View App.java
package spike;
import com.sun.net.httpserver.Filter;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
View certificates-in-java-cacerts.bat
REM install a certificate
REM =======================
set KEYTOOL=C:\Workspace\opt\Java\jdk1.8.0_162\bin\keytool
set FILE=D:\home\kristof\Downloads\wildca.cer
set KEYSTORE=C:\Workspace\opt\Java\jdk1.8.0_162\jre\lib\security\cacerts
set ALIAS=certinga.wildca.intermediate
%KEYTOOL% -import -trustcacerts ^
-alias %ALIAS% ^
View Scratch.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;
@Crydust
Crydust / FindByX.java
Created Jul 19, 2018
java jdbc findByX select query lambda
View FindByX.java
//usage
final List<String> x = findByX(
"select dummy FROM dual WHERE dummy = ?",
ps -> ps.setString(1, "X"),
rs -> rs.getString("dummy"));
private static <T> List<T> findByX(String sql, ThrowingConsumer<PreparedStatement> parameterSetter, ThrowingFunction<ResultSet, T> rowMapper) {
try (final Connection con = ds.getConnection();
final PreparedStatement ps = con.prepareStatement(sql)) {
ps.setQueryTimeout(10 /*seconds*/);
@Crydust
Crydust / GzipHelper.java
Created May 25, 2018
gzip compress and dencompress byte arrays in java 8
View GzipHelper.java
public static byte[] compress(final byte[] bytes) throws IOException {
if (bytes == null || bytes.length == 0) {
return new byte[0];
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final OutputStream gzip = new GZIPOutputStream(out)) {
gzip.write(bytes);
}
return out.toByteArray();
@Crydust
Crydust / Chronometer.java
Last active Aug 13, 2018
Simple chronometer in java
View Chronometer.java
import java.util.concurrent.TimeUnit;
public class Chronometer implements Comparable<Chronometer> {
boolean started = false;
boolean stopped = false;
long start;
long end;
public void start() {
started = true;