Monthly Archives: May 2014

Windows: Bypass proxy max size limit using cURL

Bypass proxy max size limit using cURL


Your entreprise/college proxy forbid you to download large file ? Here might be the solution.

cURL

First you need to download cURL
Go to the download page and choose your file, I personally used : “Win64 ia64.zip” from Don Luchini

Chunk file evaluation

Evaluate your proxy limit (We take 150M for this example) –> 150*1024*1024 = 157286400

Evaluate the number of chunk you’ll need : FileSize/ProxyLimit
* For a 400M file would be : 400/150 = 2,667 –> 3

So we’ll use 3 chunks:

  1. 0-157286400
  2. 157286400+1 – 157286400*2
  3. (157286400*2)+1-end

So we’ll need 3 commands :

curl --range 0-<strong>157286400</strong> <em>FILENAME</em> --output 1.ext
curl --range 157286401-314572800 <em>FILENAME</em> --output 2.ext
curl --range 314572801- <em>FILENAME</em> --output 3.ext

Merge chunks

Open a command prompt and merge the files :

copy /B 1.ext+2.ext+3.ext <em>REAL_FILENAME.REAL_EXTENSION</em>

And here you go.

Note that if you want to use a proxy with cURL simply add :

--proxy [protocol://][user:password@]proxyhost[:port]

JAD: Recursive Java decompilation command

Recursive JAD decompilation

To decompile all the classes of a jar file, unzip the jar in a folder.

Then use this command:

jad -d . -s java -r **/*.class
 
  -d <dir> - directory for output files 
  -s       - output file extension (default: .jad) 
  -r       - restore package directory structure

JAD can be found here

JAVA 8 : Lambda Expressions. Tutorial, example

Lambda Expressions

Introduction

JAVA 8 introduces a new language : Lambda Expressions

It’s meant to simplify the syntax when one use an anonymous class containing only one method. These interfaces are known as function interfaces.

 

Use case

Suppose you have a list of Person (age, last-name, first-name) and you’re asked to create a method that filters and then prints that list from different criteria.

 

Pre java 8 approach

The idea is to create a method that will filter a list of Person depending on specific attributes.
To avoid having one method per export (exportAdults, exportChildren…) we’ll introduce an interface PersonFilter with one method :
boolean filter(Person p) that will return true if the given person is part of the export we want.

Let’s go:

private static void exportSpecificPopulation(String category, Collection&lt;Person&gt; persons, PersonFilter filter) {
  for (Person p : persons) {
    if (filter.filter(p)) {
      System.out.println(category + ": "+p.getLastname() + " "+ p.getFirstname() + " " + p.getAge());
    }
  }
}

The code to call this method would be:

// Export Adults
exportSpecificPopulation("adults", persons, 
  new PersonFilter {
    @Override
    public boolean filter(Person person) {
      return p.getAge()>=ADULT_LEGAL_AGE;
    }
  }
// Export Children
exportSpecificPopulation("children", persons, 
  new PersonFilter {
    @Override
    public boolean filter(Person person) {
      return p.getAge()<ADULT_LEGAL_AGE;
    }
  }

Not that readable huh! and only to get adults and children.

Java 8 approach

In Java 8 we keep the main export method and the interface. But since the interface only has one method Lambda Reflections can be used.

The example in Java 8


Here is how we use the Lambda Expressions in our example (See Lambda Expressions syntax below) :

exportPopulation("adults", persons, p -> p.getAge()>=ADULT_LEGAL_AGE);
exportPopulation("children", persons, p -> p.getAge()<ADULT_LEGAL_AGE);

Note that it’s equivalent to :

exportPopulation("adults", persons, (Person p) -> {return  p.getAge()>=ADULT_LEGAL_AGE;});
exportPopulation("children", persons, (Person p) -> {return  p.getAge()<ADULT_LEGAL_AGE;});

Lambda expression syntax

(from oracle)

A lambda expression consists of the following:

  • A comma-separated list of formal parameters enclosed in parentheses.

    Note: You can omit the data type of the parameters in a lambda expression. In addition, you can omit the parentheses if there is only one parameter.

  • The arrow token, ->
  • A body, which consists of a single expression or a statement block. This example uses the following expression:

    If you specify a single expression, then the Java runtime evaluates the expression and then returns its value.

    A return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:

    email -> System.out.println(email)

Note that a lambda expression looks a lot like a method declaration; you can consider lambda expressions as anonymous methods—methods without a name.