Archivio della categoria: Programmazione

Convert InputStream to String in android, java

public String convertStreamToString(InputStream is)

throws IOException {

//

// To convert the InputStream to String we use the

// Reader.read(char[] buffer) method. We iterate until the

// Reader return -1 which means there's no more data to

// read. We use the StringWriter class to produce the string.

//

if (is != null) {

Writer writer = new StringWriter();

 

char[] buffer = new char[1024];

try {

Reader reader = new BufferedReader(

new InputStreamReader(is, "iso-8859-1"));

int n;

while ((n = reader.read(buffer)) != -1) {

writer.write(buffer, 0, n);

}

} finally {

is.close();

}

return writer.toString();

} else {

return "";

}

}

.htaccess redirect from olddomain to newdomain

A friend of mine wanted to redirect an old domain to a new doman, he made a simple 301 redirect but as the server physical ip was the same he falled into an redirect loop between domains.

What he needed to fix that is an if condition in the rewrite rule, I made this snipplet for him.

RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]

Webkit custom scrollbar iOs style

I wanted to reproduce the look and feel of the ios scrollbar, so I played with the
::-webkit-scrollbar property.

Check out the demo, here

Here the code:

::-webkit-scrollbar {
width: 10px;
height: 10px;
}
::-webkit-scrollbar-button:start:decrement,
::-webkit-scrollbar-button:end:increment {
height: 30px;
display: block;
background-color: transparent;
}
::-webkit-scrollbar-track-piece {
background-color: #E9E9E9;
-webkit-border-radius: 6px;
}
::-webkit-scrollbar-thumb:vertical {
height: 50px;
background-color: #999;
border: 1px solid #eee;
-webkit-border-radius: 6px;
}

Tip: Fix random zoom on ipad orientation change

If you have the width set in the viewport :

<meta name = "viewport" content = "width=device-width; initial-scale=1.0;
 maximum-scale=1.0;"
/>

And then change the orientation it will randomly zoom in sometimes (especially if you are dragging on the screen) to fix this don’t set a width here I used :

<meta id="viewport" name="viewport" content="initial-scale=1.0; user-scalable=0;
minimum-scale=1.0; maximum-scale=1.0"
/>

This fixes the zoom whatever happens then you can use either window.onorientationchange event or if you want it to be platform independant (handy for testing) the window.innerWidth method.

Tip: Use Code igniter methods without instance a class

Sometimes making a class in Code Igniter you could need to use some method of Code igniter (database class for examples) without instance the class itself, for example in static method.

In this case, you can use the Code Igniter instance itself:

$ci_ins =& get_instance();
$ci_ins->load->model("some model");
$ci_ins-->db->insert(); //Using database

Leggere le variabili GET in javascript

Snipplet per leggere le variabili GET in Javascript

function GET(q,s)
{
s = s ? s : window.location.search;
var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
return (s=s.replace(/^\?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}

Esempio:

// http://test.com/index.php?v=test
var var1 = GET('v');

alert(var1);

Ho trovato questa ottima funzione qui
Credits tuttavia andava fixata la regex, per questo la posto.