Skip to content

Instantly share code, notes, and snippets.

View games647's full-sized avatar

games647 games647

View GitHub Profile
@games647
games647 / round-half-up.rkt
Created November 23, 2016 14:27
In racket the round method rounds to the next even number. This procedure rounds to the nearest neighbor.
;; round-half-up: number -> integer
;;
;; Rounds the number to the nearest neighbor
;;
;; Example: (round-half-up 2.5) = 3
(define (round-half-up number) (floor (+ number .5)))
;; Tests
(check-expect (round-half-up 2.5) 3)
(check-expect (round-half-up 3) 3)
@games647
games647 / settings.xml
Created December 4, 2016 13:09
Change the location of your local maven repository. Place this file into ~/.m2 and then maven use the specified folder for dependency management
<settings>
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
-->
<localRepository>/path/to/local/repo</localRepository>
</settings>
@games647
games647 / AgentLoadingRuntime.java
Created December 9, 2016 12:01
Loads a java agent at runtime
import com.github.games647.JarUtils;
import com.sun.tools.attach.VirtualMachine;
import java.lang.management.ManagementFactory;
public class AgentLoadingRuntime {
public static void loadAgent(String[] args) {
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');
@games647
games647 / HeapDump.java
Created December 9, 2016 12:04
Dumps the JVM heap stats to a single string or in a file which then can be analyzed by VisualVM or Java Mission Control
import java.io.File;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
@games647
games647 / FlightRecorder.java
Created December 9, 2016 12:06
Starts a new Java Flight Recording and dump the results to a file. It then can be analyzed by Java Mission Control.
import java.io.File;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
@games647
games647 / hasher.java
Created December 9, 2016 12:08
Hash a input string and return the hash as string
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class JavaHasher {
private static final String HASH_ALGO = "SHA512";
public String hashInput(String input) {
String hashed = null;
@games647
games647 / FormatBytes.java
Created December 9, 2016 12:10
Format the amount bytes to a human readable format.
/*
* Example output:
SI BINARY
0: 0 B 0 B
27: 27 B 27 B
999: 999 B 999 B
1000: 1.0 kB 1000 B
1023: 1.0 kB 1023 B
1024: 1.0 kB 1.0 KiB
@games647
games647 / BCryptHasher.java
Created December 9, 2016 12:15
Generates BCrypt hashes in Java using: http://www.mindrot.org/projects/jBCrypt/ This snippet fixes compatibility with BCrypt hashes generated in PHP
import org.mindrot.jbcrypt.BCrypt;
public class BCryptHasher {
public static String hashPassword(String rawPassword) {
//generate a different salt for each user
return BCrypt.hashpw(rawPassword, BCrypt.gensalt());
}
public static boolean checkPassword(String passwordHash, String userInput) {
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
@games647
games647 / NetworkPreparedStatements.java
Created December 9, 2016 12:21
Used named placeholders instead of indexes for prepared statements
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;