Skip to content

Instantly share code, notes, and snippets.

package misc;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class ArrayTools
{
private static class ValueComparator<K , V extends Comparable<V>> implements Comparator<K>
{
@scottcagno
scottcagno / MessageDecoder.java
Last active August 29, 2015 14:17
Message decoder for netty4...
public class MessageDecoder extends LineBasedFrameDecoder {
private static final byte D = (byte)':';
public MessageDecoder(int maxLength) {
super(maxLength);
}
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
ByteBuf m = (ByteBuf) super.decode(ctx, buffer); // run newline decoder
@scottcagno
scottcagno / Bytes.java
Last active August 29, 2015 14:18
A simplistic byte[] wrapper that has better memory performance than a ByteBuffer... easy to use with collections like HashMap
import java.nio.charset.StandardCharsets;
public final class Bytes {
protected final byte[] buffer;
public Bytes(byte[] buffer) {
this.buffer = buffer;
}
@scottcagno
scottcagno / MMapFile.java
Last active August 29, 2015 14:22
Block Style Memory Mapped File Utility Class
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
/**
* Created by Scott Cagno.
* Copyright Cagno Solutions. All rights reserved.
*/
@scottcagno
scottcagno / MergeObjects
Last active August 29, 2015 14:23
Merge target and source objects
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
@scottcagno
scottcagno / OpenFile
Created September 24, 2015 02:18
Open / Create File
package main
import (
"log"
"os"
"path"
)
func main() {
@scottcagno
scottcagno / get-golang.sh
Last active January 12, 2017 21:51
Download, install, export and stage Go for linux
#!/usr/bin/env bash
# CHANGE THIS TO THE VERSION YOU WANT TO INSTALL
VERSION="1.7.4"
UPDATED="false"
# CHECKING TO SEE IF WE ARE UPDATING OR INSTALLING NEW
CURRENT=`echo \`go version\` | grep -o '1\..'`
if (( ${VERSION##*.} > ${CURRENT##*.} )); then
UPDATED="true"
@scottcagno
scottcagno / go_httpcors_handler.go
Created October 16, 2015 17:27
golang http cors handler/wrapper
func HandleCORS(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
fn(w, r)
}
}
@scottcagno
scottcagno / gist:f5728d90446fd080ed87
Created November 14, 2015 00:13 — forked from mcastilho/gist:e051898d129b44e2f502
Cheap MapReduce in Go
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
@scottcagno
scottcagno / audoconversionwalktree.sh
Created December 5, 2015 08:55
walk directory tree and convert any audio files (mp3, aif, aiff, flac, caf, m4a, etc.) to wav format @ 44.1Khz 24bit audio
#!/bin/bash
# EXAMPLE OPTIONS
#ffmpeg -i $file -ar 44.1k -ab 320k "${file%.mp3}.wav" && rm "$file"
#sox $file -b 24 -r 44100 -c 2 "${file%.mp3}.wav" norm && rm "$file"
function rmspaces {
local dir=$1
for file in "$dir"/*; do
[ -f "$file" ] && ( mv "$file" "$(echo $file | sed -e 's/ /_/g')" )