File Locking!!! Is this a BUG or there is something wrong in my code.

In new JDK v 1.4. new means of file locking are introuced, and it seems it is a little bit buggy yet (or maybe this one isn't a one). though it's better than nothing :)
here is 2 classes. first locks a file, the second attemts to change it.
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
class FileLocker {
  static File targetFile = new File ("D:\\Java",
                                     "manual.chm");
  public static void main(String[] args)
      throws Exception
    RandomAccessFile fin = new RandomAccessFile (targetFile, "rw");
    FileChannel fChan = fin.getChannel();
    FileLock lock = fChan.lock();
    // to let other process to attempt to change the same file.
    Thread.currentThread().sleep(100000);
    lock.release();
    fChan.close();
    fin.close();
}and the other class
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
class FileWriter {
  static File targetFile = new File ("D:\\Java",
                                     "manual.chm");
  public static void main(String[] args)
      throws Exception
    // this is better/more secure way to access files.
    RandomAccessFile f = new RandomAccessFile (targetFile, "rw");
    // next line deletes the file content even if file locked.
//    FileOutputStream f = new FileOutputStream (targetFile);
//--this parts is not needed as the problem is in first 2 lines
    FileChannel fChan = f.getChannel();
    if (fChan.isOpen()) {
      System.out.println("File is Opened, can't write");
    } else
      f.write(-1);
}so when i use FileInputStream for open stream to write,
constructor deletes the targetFile (makes it size equal to 0)
at once i create new FileInputStream object. Since, when i
use RandomAccessFile to open stream for writting nothing happens.
after that, f.write (-1); throws exception regardless the way the
uoutput stream was created.
So i think there is some problem in FileInputStream class' constructor.

OK, I know you mean FileOutputStream instead of FileInputStream in the above message. Looks to me like you've discovered a bug in the way Java is locking the file - locking the whole contents of a file (using the no-args lock or tryLock methods) doesn't prevent other programs, including Java, from overwriting some, if not all, of the file. Note that this occurs even on Windows 2000, where exclusive locks appear to be well supported.
I've found a similar problem where saving from Outlook appears to zero the whole of the locked file. Saving from other programs that are a little more careful, such as XEmacs, appears to only (!) truncate the file to 0, as the FileOutputStream does.
From what I can tell, in JDK 1.4.2 RandomAccessFiles respect each others' locks, but nothing else does.

Similar Messages

  • HT4061 How do you unlock your AppStore Lock, seriously this is bugging me because the App Store costumer service is down , APPLE HELP ME!!!

    How do you unlock your AppStore Lock, seriously this is bugging me because the App Store costumer service is down , APPLE HELP ME!!!

    You posted in the iPad forum instead of the iPhone forum. To get answers to your question, next time post in the proper forum. See https://discussions.apple.com/index.jspa  I'll request that Apple relocate your post.
     Cheers, Tom

  • My iPhone is in recovery mode, But once it's done with the process a tab comes up saying there was something wrong restoring it (1015) error. Any suggestions of how I could fix this?

    My iPhone is in recovery mode, But once it's done with the process a tab comes up saying there was something wrong restoring it (1015) error. Any suggestions of how I could fix this?

    Error 1015: This error is typically caused by attempts to downgrade the iPhone, iPad, or iPod touch's software. This can occur when you attempt to restore using an older .ipsw file. Downgrading to a previous version is not supported. To resolve this issue, attempt to restore with the latest iPhone, iPad, or iPod touch software available from Apple. This error can also occur when an unauthorized modification of the iOS has occurred and you are now trying to restore to an authorized, default state.
    In other words:
    The backup to which you're trying to restore was created with a lower or previos version of iOS than the one installed on your iPhone during the recovery.  If this is the case then you'll be unable to restore your phone with that backup and you'll have to set your iPhone up as new.  Or,
    Your iPhone was/is jailbroken.  If this is the case, there is nothing anyone here or at Apple can do for you.

  • I have a Mac OS X version 10.3.9, I cannot find a browser that is up to date and will play any video type files. Could anyone tell me if there is something up to date?

    I have a Mac OS X version 10.3.9. I cannot find a browser that is up to date or one that will play any video type files. Could anyone tell me if there is something up to date? or can the software be updated on this computer to get up to date?

    Visit MacUpdate or CNET Downloads and search for browsers. However, I doubt you will find any contemporary browsers that work with Panther.
    Have you considered upgrading your OS, if possible, or getting a newer computer?
    BTW, you've posted this in the Servers and Enterprise Software forum. In the future you should post in the appropriate forum for you question. This forum isn't the right one. Mac OS X v10.3 and earlier would be the right place to start.

  • I downloaded Lion and  have attempted to 'updat' it from apps to my imac... after 3 hours of uploading it show mw an error message saying there's something wrong with my internet connection... which there is not. Anyone else experience this?

    I downloaded Lion and  have attempted to 'update' it from apps to my imac... after 3 hours of uploading it show me an error message saying there's something wrong with my internet connection... which there is not. Anyone else experience this?

    What type of Internet connection do you have?  How fast is it?

  • Safari tries repeatedly to open a page. The page partially opens but then Safari reports that there's something wrong with page and I get an error message. This is happening at too many web sites for one site to be having issues.

    Safari tries repeatedly to open a page. The page partially opens but then Safari reports that there's something wrong with page and I get an error message. This is happening at too many web sites for one site to be having issues.

    I have a similar problem, Safari cannot load a complet page. It stalls e.g. ["38 of 42"]. sometimes it will complete loading after a long wait. just as often it will never complete the page,
    The same thing happens in chrome and firefox but safari on my iPad works just fine.
    I have the latest version of everything
    I assume there is a system problem but cleaning up permissions does not help

  • I am apparently using a crazy amount of data this month.  Could there be something running in the background on my device?

    I am apparently using a crazy amount of data this month.  Could there be something running in the background on my device?

    As mama23dogs said check data usage, if you see any spikes you can drag the vertical lines in to that range and it will update the apps for that time frame. 

  • Turned my iphone 4 off now I turned it on again and stuck on apple logo, after this i put it in recovery mode but it failed and itunes says there is something wrong with my iphone any ideas

    My iphone 4 stuck on the apple logo then it wont work in recovery mode. My iphone then wouldnt work in recovery mode, itunes said there is something wrong with my iphone. On my iphone it says connected to itunes any ideas what could be wrong

    You might have to get it serviced, or visit an Apple Store to get it looked at.
    Apple - Support - Service Answer Center

  • An Internal error has occurred: There is something wrong in CryptuprotectData

    When I try to upload to Facebook, I receive the following error message: "An Internal error has occurred: There is something wrong in CryptuprotectData". How do I fix it?

    Your client is CR R1, your server is XI R2. This is a known bug. To fix you need BOE XIR1 SP3 or above. I'm not sure this was fixed in a CR service pack. Sorry I can't find a link for that right now.
    Or use [this link|http://resources.businessobjects.com/support/additional_downloads/service_packs/crxir2.asp] to get CR R2, uninstall CR R1, and replace with CR R2. NOTE: they should still use the same license keys. If using CR to XIR2 you should always keep the same version so this is the preferred method.
    Regards,
    Tim

  • There is something wrong with the volumes buttons in my macbook pro, every time i pressed the one who raises the volume, it leads me to the screen where (i do not no what its called) the background is black with the date and time and a calculator.

    There is something wrong with the volumes buttons in my macbook pro, every time i pressed the one who raises the volume, it leads me to the screen where (i do not no what its called) the background is black with the date and time and a calculator. However, when i lower it, my safari tab goes out of the screen. What do you guys think i should do? I'm getting very nervous.

    hey HAbrakian!
    You may want to try using the information in this article to adjust the behavior of your function keys to see if that resolves the behavior:
    Mac OS X: How to change the behavior of function keys
    http://support.apple.com/kb/ht3399
    Take care, and thanks for visiting the Apple Support Communities.
    -Braden

  • There's something wrong with my iphone 4s

    So there's something wrong with my iPhone 4s.
    Just recently it worked fine and all of a sudden it started going insane so what I did was turn my phone off for five minutes then back on. When I did my imessage won't work nor does my app called Line won't allow me to log in claiming my network is not working properly (when in fact it is because I used safari and searched five websites and it works fine) then I tried my imessage and it still won't work so I went to my settings and when I did it said my imessage was off so I switched it on and it was asking for my apple ID and I gave the correct information, making sure to type the password slowly so I won't make errors but still it claims I have the wrong password. I have other apple IDs and I used them and they all have the same password and still nothing. It's really annoying and I want it to be fixed soon because I've tried everything, I've tried putting it on airplane mode then putting it off, turning off imessage and restarting then turning it on again, I can't even log on to my apple ID here properly a few seconds ago and I restarted my phone already and shut it off too and still no difference.
    I'm hoping someone who can help me, please  nothing that requires deleting files because my files are important I do remember backing up on iCloud but I'm not quite sure when I did that and so I'm afraid of using that back up file.

    Hi Rsh Ahmed,
    Thanks for visiting Apple Support Communities.
    If you're not able to connect to wi-fi networks after updating, try the steps in our wi-fi troubleshooting article:
    iOS: Troubleshooting Wi-Fi networks and connections
    http://support.apple.com/kb/ts1398
    All the best,
    Jeremy

  • There is something wrong with the forum

    There is something wrong with the forum. it is messed up.!
    I been a little bit observant this past few days, I observed alot of problems..
    here are a few, ( there are actually alot more, but I could not describe it. its just very weird.
    I thought someone hacked to my profile and used it to ask that one, but when I clicked on it, it was just one of the questions I have responded to.
    sometimes I can not post to threads, an error would come up, most especially if I reply to a reply,
    sometimes, when I Post images it would work the first time I post it, but when I search for it again, only the links are given, no images anymore.
    sometimes I get booted out randomly, in the middle of writing a reply
    sometimes the posts switches from threaded to flat , flat to threaded, and sometimes both.
    sometimes when I checked on the stuffs>discussion, the post is highlighted but there is no new response or like, and even there was, I can see which one is it, especially to the posts that had many reply already
    sometimes it still sends email to my gmail eventhough i have unsubscribe to the post already,
    sometimes get error 504, and some other numbers I forgot.
    sometimes during writing a response the blinking thing that indicates where are you writing goes missing or on the wrong place.
    OH come on..! I am trying to help, you don't want one?
    and don't blame it on ,my browser ( i checked and used IE9, FIREFOX, and even CHROME), or to my internet connection.( i got the best there is for home connections), or to my laptop ( it's a little bit old, but it still kicks butt holes) and tried it already with other pc/internet connection/and whatnot.
    I am still sometimes visiting my previous home (NOKIA FORUM) and I don't experience those, and we even have live chat over there,
    this forum needs a revamp. and how I wish this could be repaired via Sony Update Service, or PC Companion, but of course if it was possible I would have done it already.
    We try to help users, make the forum conducive for users trying to help users. \m/
    best of luck,,
    MARLON LB :smileywink:

    This is how the forum looks for me.
    But during around midnight, (gmt +8) the forum changes to this

  • ITunes is telling me there is something wrong with my iPhone, what should I do? I have the 16GB iPhone 4 and I bought it 2nd hand with no reciept so I cant send it off. What should I do?

    iTunes is telling me there is something wrong with my iPhone, what should I do? I have the 16GB iPhone 4 and I bought it 2nd hand with no reciept so I cant send it off. What should I do? This happens after i updated it and I click sync.

    Pilko95 wrote:
    iTunes is telling me there is something wrong with my iPhone,
    Can you explain exactly what the message says?
    Do you have its serial number? If you do, type it in the following web page to determine your service and support options:
    https://selfsolve.apple.com/agreementWarrantyDynamic.do
    If you do cannot find a serial number on the iPhone you can find it stored in your last iTunes backup:
    Open iTunes on the computer you used to sync the iPad and select Preferences…from the iTunes menu. (In Windows, choose Edit > Preferences.)
    Select the Devices pane. Position the cursor over any of its backups. The iPad's serial number will appear in a popup window.
    For a screenshot read: iOS: How to find the serial number, IMEI, MEID, CDN, and ICCID number
    Scroll down to:
    Additional Information >
    Getting the information from your computer >
    If you have access to your computer, but not your device

  • How do I import my photos from my iPhone to iPhoto, when I press import I just get a message saying there is something wrong with one of my photos and disconnects my iPhone.

    How do I import my photos from my iPhone to iPhoto, when I press import I just get a message saying there is something wrong with one of my photos and disconnects my iPhone.

    I couldn't find Image Capture at my first try, butI will try again. Do you inow how I can find the corrupted photo on my iPhone? Im a bit lost..
    Sorry, I just noticed, I made a mistake. "Image Capture" is in the Applications folder, not Utilities. And it may have a different name on your Mac, if your system language is not English:  Look for the icon with the camera:
    When Image Capture can import from your iPhone, spilt the folder with the imported photos in several small subfolders and try to import them separately to iPhoto. This way, you can identify a subset of photos with the corrupted one.
    Then try to import from that small subset one by one, until you hit the culprit. And then delete that photo or video from the phone.

  • When I open my MacBook Air to log on it dims and then lights up and deletes my password as I am typing it. Could there by something wrong?

    As I open it to type in my password it'll dim and light up and then when it lights up again, it'll delete my password. It's not a huge deal. I don't mind retyping my password, but it makes me think there is something wrong with it since it does this. It's not a significiant change in display, it's almost not noticeable. I bought this Mac about one month ago, so I'm hoping I have not done something to damage it already. Thanks!

    Interestingly, it appears you did manage to get your podcast accepted in the Store - if you did this with an .html feed I'm a bit surprised it worked. I wonder whether you started with an .xml URL and it got changed by accident?
    If you did manage to get the Store to accept a feed with an .html extension then you will need to change its attention to the revised version of the feed. This will mean that you will have to leave the present feed where it is, upload a version with the correct extension (having checked it works) and then add the 'itunes:new-feed-url' tag to the incorrect feed as described in this page:
    http://www.wilmut.webspace.virginmedia.com/notes/podcast.html#move
    Of course if you managed to change the extension by accident then changing it back should do the job.

Maybe you are looking for