Skip to content

Instantly share code, notes, and snippets.

@thiagoh
thiagoh / ExtendedInputStream.java
Last active August 29, 2015 14:27
Input stream with multiple sources
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@thiagoh
thiagoh / GenerateCipheredText
Created September 1, 2015 22:58
Java class that generates a DESede ciphered code so that I can my "crypto-c" repository (encryptation in java decryptation with crypto-c) https://github.com/thiagoh/crypto-c
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class GenerateCipheredText {
@thiagoh
thiagoh / how-to-use-promises.js
Last active September 25, 2015 00:12
a good example of how to use promises
var _loadPortletsData = function(page) {
var deferred = $q.defer(); // or other deferred object like jQuery.Deferred();
var promise = deferred.promise;
var count = 0;
_.map(page.columns, function(column) {
_.map(column.portlets, function(portlet) {
++count;
@thiagoh
thiagoh / tg-bind-safe-html.js
Last active December 18, 2015 02:39
Directive to bind safe html in AngularJS
// bind safe html in AngularJS
angular.module('myModule', ['ngSanitize'])
.directive('tgBindSafeHtml', ['$sce', '$sceDelegate', '$parse', '$compile',
function($log, $sce, $sceDelegate, $parse, $compile) {
return {
restrict: 'A',
compile: function ngBindHtmlCompile(tElement, tAttrs) {
var ngBindHtmlGetter = $parse(tAttrs.tgBindSafeHtml);
var ngBindHtmlWatch = $parse(tAttrs.tgBindSafeHtml, function getStringValue(value) {
int t1 = (100 - 128) >> 31;
int t2 = (200 - 128) >> 31;
System.out.println(Integer.toBinaryString(t1));
System.out.println(Integer.toBinaryString(t2));
System.out.println();
int t = 1;
System.out.println(Integer.toBinaryString(t) + " L shifted by 0:\t" + Integer.toBinaryString(t << 0));
@thiagoh
thiagoh / wraps.py
Created April 11, 2016 14:36
wrapped functions: how it works
# https://docs.python.org/2/library/functools.html#functools.update_wrapper
from functools import wraps
def wrapped_func1(func):
def __inner_func():
return func()
return __inner_func
def wrapped_func2(func):
@thiagoh
thiagoh / ReferencesTest.java
Created July 7, 2016 17:55
WeakReference example
package com.liferay.portal.service;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
public class ReferencesTest {
@thiagoh
thiagoh / php-7-closures.php
Created October 20, 2016 23:20
PHP 7 Closures example
php > $x = 123;
php > $var4 = static function() use(&$x) {return $x;}; // with 'static'
php > echo $var4();
123
php > echo (function() use(&$x) {return $x;})();
123
php > $var4 = (function() use(&$x) {return $x;}); // without 'static'
php > echo $var4();
123
php > $x = 321;
@thiagoh
thiagoh / bash.generate.random.alphanumeric.string.sh
Created November 19, 2016 19:52 — forked from earthgecko/bash.generate.random.alphanumeric.string.sh
shell/bash generate random alphanumeric string
#!/bin/bash
# bash generate random alphanumeric string
#
# bash generate random 32 character alphanumeric string (upper and lowercase) and
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
# bash generate random 32 character alphanumeric string (lowercase only)
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
var createPromise = function(label, time) {
return function() {
return new Promise(function(resolve) {
setTimeout(function() {
console.log(label);
resolve();
}, time);