required standard open options for creating or replacing a file


I just found a bug on my own code.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE, // create if not exist
    StandardOpenOption.WRITE // i'm gonna write on
);

Above code seemed working, but when I tried to replace a bigger file with a smaller one, the file size remains and the only leading(?) portion of file contents replaced.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE,
    StandardOpenOption.TRUNCATE_EXISTING, // truncate the file!!!
    StandardOpenOption.WRITE
);

java_home environment variable


Windows

N/A

OS X

installations

$ ls -l /Library/Java/JavaVirtualMachines/
....
$

/usr/libexec/java_home

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdkx.y.z_mm.jdk/Contents/Home

$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_mm.jdk/Contents/Home

$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_mm.jdk/Contents/Home

~/.profile

$ cat ~/.profile
...
export JAVA7_HOME=$(/usr/libexec/java_home -v 1.7)
export JAVA8_HOME=$(/usr/libexec/java_home -v 1.8)
export JAVA_HOME=$JAVA8_HOME
...

$ echo $JAVA7_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home

$ echo $JAVA8_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home

$

Ubuntu

See installing latest oracle jdk in old ubuntu.

mapping a milliseconds parameter directly into a date instance in jax-rs


@POST
public void myResource(
    @QueryParam("milliseconds")
    @NotNull
    final Long milliseconds) {

    final Date date = new Date(milliseconds);
    myService(date);
}

I wrote some codes using ParamConverter and ParamConverterProvider.
And now I can do like this.

@POST
public void myResource(
    @QueryParam("milliseconds")
    @NotNull
    @MillisecondsParam
    final Date date) {

    myService(date);
}

Here are 3 classes for this.

curl을 이용해 바이너리 파일 올릴 때 주의할 점


curl을 이용해서 PUT 메서드를 테스트하던 중 데이터가 이상하게 올라가는 현상을 발견했습니다.

$ curl -X PUT -H "Content-Type: image/jpeg" --data @a.jpg http://...

올라가긴 잘 올라가는 것 같은데 브라우저로 이미지를 불러보면 안보입니다.
검색결과, --data 옵션은 --data-ascii와 같다고 하는 군요.
다음과 같이 했더니 잘 올라갑니다.

$ curl -X PUT -H "Content-Type: image/jpeg" --data-binary @a.jpg http://...

changing a nullable milliseconds to a nullable date using optional.


public Date toDate(@Nullable Long millis) {
    //return millis == null ? null : new Date(millis);
    return Optional.ofNullable(millis).map(Date::new).orElse(null);
}

Returning an Optional rather than a nullable Date instance is a better idea.

public Optional<Date> toDate(@Nullable Long millis) {
    return Optional.ofNullable(millis).map(Date::new);
}

selecting javac with maven in command line


I just found that some of my company’s legacy codes doesn’t compile with JDK 8. And I just tried selecting the javac executable in command line rather than changing maven-compiler-plugin‘s configuration.

$ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/path/to/jdk7/javac clean package

Note that, as documented, maven.compiler.fork parameter must be true.

suppressing application.wadl with jax-rs


@PreMatching
@Provider
public class EpdResourcesRequestFilter implements ContainerRequestFilter {

    @Override
    public void filter(final ContainerRequestContext requestContext)
        throws IOException {

        // abort any request whose uri includes ".wadl" with 404
        if (requestContext.getUriInfo().getPath().contains(".wadl")) {
            requestContext.abortWith(Response.status(Status.NOT_FOUND).build());
        }
    }

}