Categories
discuss

When using Java’s FileLock, is it ok to let close() to automatically do a lock.release()?

As most should know close() also closes any streams uses.

This allows the follow code:

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(...)));
...
br.close();

This is nice, since we don’t need a reference to FileInputStream and remember to close it.

But does it also work for FileLocks?

final FileInputStream fis = new FileInputStream(new File("buffer.txt"));
final FileChannel c = fis.getChannel();
final FileLock lock = c.lock(0L, Long.MAX_VALUE, true);
final BufferedReader br = new BufferedReader(new InputStreamReader(fis));

try {
    while(br.ready()) {
        System.out.println(br.readLine());
    }
} finally {
    br.close();
}

I’ve tried this code and the lock is correctly released when br.close() is called, but is is safe to do so? The Closeable JavaDoc says, “Closes this stream and releases any system resources associated with it.” Am I safe to assume that I am using close() as specified to release() the lock?

Answer

According to the JavaDoc:

It remains valid until the lock is released by invoking the release method, by closing the channel that was used to acquire it, or by the termination of the Java virtual machine, whichever comes first.

And here are the contents of FileInputStream.close()

public void close() throws IOException {
    if (channel != null)
        channel.close();
    close0();
}

It looks like close on the stream closes the channel which releases the lock.

Categories
discuss

Most efficient javascript/AJAX toolkit?

What is the most efficient Javascript/AJAX toolkit?

Answer

Choose the library that makes the most sense to you idiomatically.

The differences in efficiency are going to become less and less important as two things happen.

  1. Browsers are getting much better at interpreting Javascript.
  2. Most major Javascript libraries are planning to adopt a single selector engine, Sizzle
Categories
discuss

invalid header file while using jar for archiving

When i use this cmd line : jar cmf arshad.mf ars.jar *.class i get this error :

invalid header field name:Manifest-version

This is my manifest file :

Manifest-Version: 1.0
Main-Class:t

i made the manifest file with notepad in UTF-8 encoding – is there any problem with the manifest ?

Answer

Add a space after the colons:

Manifest-Version: 1.0
Main-Class: t
Categories
discuss

How to get JavaScript function data into a PHP variable

I am using PHP and JavaScript. My JavaScript code contains a function, get_data():

function get_Data(){
    var name;
    var job;
    .....

    return buffer;
}

Now I have PHP code with the following.

<?php
    $i=0;
    $buffer_data;

    /* Here I need to get the value from JavaScript get_data() of buffer;
       and assign to variable $buffer_data. */
?>

How do I assign the JavaScript function data into the PHP variable?

Answer

Use jQuery to send a JavaScript variable to your PHP file:

$url = 'path/to/phpFile.php';

$.get($url, {name: get_name(), job: get_job()});

In your PHP code, get your variables from $_GET['name'] and $_GET['job'] like this:

<?php
    $buffer_data['name'] = $_GET['name'];
    $buffer_data['job']  = $_GET['job'];
?>
Categories
discuss

How does Java’s PreparedStatement work?

I am planning to replace repeatedly executed Statement objects with PreparedStatement objects to improve performance. I am using arguments like the MySQL function now(), and string variables.

Most of the PreparedStatement queries I have seen contained constant values (like 10, and strings like "New York") as arguments used for the ? in the queries. How would I go about using functions like now(), and variables as arguments? Is it necessary to use the ?s in the queries instead of actual values? I am quite confounded.

Answer

If you have variables use the ‘?’

int temp = 75;
PreparedStatement pstmt = con.prepareStatement(
    "UPDATE test SET num = ?, due = now() ");
pstmt.setInt(1, temp); 
pstmt.executeUpdate():

Produces an sql statment that looks like:

UPDATE test SET num = 75, due = now();
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..