Skip to content

Instantly share code, notes, and snippets.

View zhoulifu's full-sized avatar

zhoulifu zhoulifu

View GitHub Profile
@zhoulifu
zhoulifu / gradlew
Last active August 4, 2017 08:15
A wrapper of gradle wrapper for saving time from typing './'
#!/usr/bin/env bash
echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }
abort() {
echoYellow "Command 'gradle' not found in your PATH."
echoYellow "And could not found 'gradlew' in current directory."
echoYellow "Abort."
exit 1
@zhoulifu
zhoulifu / Hexdump.java
Created April 28, 2017 07:42
Hexdump forked from org.apache.commons.io.HexDump
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@zhoulifu
zhoulifu / BadRSV.java
Last active April 27, 2017 12:44
Demo to produce 'bad rsv 4''
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.client.WebSocketClient;
@zhoulifu
zhoulifu / ExtendPathMapping.java
Created April 12, 2017 02:23
Mapping multi-path to a handler method
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter;
@zhoulifu
zhoulifu / IdGenerator.java
Last active December 23, 2016 09:31
A high concurrency unique id generator
/**
* Simple unique id generator at high concurrency. fork from
* <a href="https://github.com/twitter/snowflake/tree/snowflake-2010">snowflake</a>.
* <br/>
* The return value of {@link IdGenerator#nextId()} is consist of 41-bit timestamp and
* 10-bit worker id and 12-bit sequence, so it could support at most 1023 workers to
* generate 4095 ids per millisecond.
* <br/>
* Because the timestamp's bit width is 41, so it could be used for about 69.7 years
* {@code ((1<<41) / (365*24*3600*1000))}. The deadline depends on the
@zhoulifu
zhoulifu / clean-zsh-history.sh
Last active April 18, 2020 00:02
clean-up the .zsh_history file
#!/bin/sh
sort -t ";" -k 2 -u ~/.zsh_history | sort -o ~/.zsh_history
@zhoulifu
zhoulifu / DiscardServer.java
Created November 17, 2016 09:29
NIO DiscardServer
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;
class Server implements Runnable {
@zhoulifu
zhoulifu / ConfigureProperties.java
Created October 18, 2016 10:04
Properties which support to refer back to previously defined values (e.g. * from system properties)
import java.util.Properties;
/**
* {@link Properties} which support to refer back to previously defined values (e.g.
* from system properties)
*
*/
class ConfigureProperties extends Properties {
private static final long serialVersionUID = -3868173073422671544L;
@zhoulifu
zhoulifu / mysql_dump.sh
Created June 17, 2016 03:15
script to do mysql backup
#!/usr/bin/env bash
# Func: mysql database backup
# Date: 2016-06-07
USER="user"
PSW="psw"
DUMP_BIN=`which mysqldump`
ADMIN_BIN=`which mysqladmin`
MYSQL_BIN=`which mysql`
public class ByteArrayBuffer {
private byte[] buffer;
private int len;
public ByteArrayBuffer() {
this(4096);
}
public ByteArrayBuffer(int capacity) {
if (capacity < 0) {