Skip to content

Instantly share code, notes, and snippets.

@amadeu01
Last active March 1, 2018 17:15
Show Gist options
  • Save amadeu01/b956216f3985b16e9076521b63764d67 to your computer and use it in GitHub Desktop.
Save amadeu01/b956216f3985b16e9076521b63764d67 to your computer and use it in GitHub Desktop.
This is me collection of small codes that have been helped me at some point in my life :D

This is my collections of helpers:

public String convert(InputStream inputStream, Charset charset) throws IOException {
 
	StringBuilder stringBuilder = new StringBuilder();
	String line = null;
	
	try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset))) {	
		while ((line = bufferedReader.readLine()) != null) {
			stringBuilder.append(line);
		}
	}
 
	return stringBuilder.toString();
}

It could be, for Java 8

public String convert(InputStream inputStream, Charset charset) throws IOException {
 
	try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset))) {
		return br.lines().collect(Collectors.joining(System.lineSeparator()));
	}
}
  • With Apache commons
public String convert(InputStream inputStream, Charset charset) throws IOException {
	return CharStreams.toString(new InputStreamReader(inputStream, charset));
}

Gson - Java serialize / deserialize

For instance, if you want a deserialize a hashmap

import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;

Type stringStringMap = new TypeToken<HashMap<String, String>>(){}.getType();
Gson().fromJson(myJSON, stringStringMap);

SQLite

SELECT sql
  FROM (
        SELECT sql sql, type type, tbl_name tbl_name, name name
          FROM sqlite_master
         UNION ALL
        SELECT sql, type, tbl_name, name
          FROM sqlite_temp_master
       )
 WHERE type != 'meta'
   AND sql NOTNULL
   AND name NOT LIKE 'sqlite_%'
 ORDER BY substr(type, 2, 1), name
SELECT sql
  FROM (
        SELECT sql sql, type type, tbl_name tbl_name, name name
          FROM sqlite_master
         UNION ALL
        SELECT sql, type, tbl_name, name
          FROM sqlite_temp_master
       )
 WHERE tbl_name LIKE shellstatic()
   AND type != 'meta'
   AND sql NOTNULL
 ORDER BY substr(type, 2, 1), name

Bash

If you want to search for a string on files you can use

find ./* -type f -exec grep -l STRING {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment