Does `javax.imageio.ImageIO` require file locking? - java

My app is saving images into a folder. Simultaneously another piece of software watches over this folder and prints pasted images.
Problem I've encountered is that printing app can send on printer half rendered image or doesn't react to saved images at all.
I'm using javax.imageio.ImageIO.write(RenderedImage im, String formatName, File output) to save BufferedImage into png format.
If locking required please provide a code example with explicit locks. If not explain why.
I've tested on Windows, JRE 1.8

Basically, you should write the image to a temporary file and then rename it to the final file name. this way the file is completely written before the other app becomes aware of it.

You probably want to implement some kind of locking mechanism that files are only read when they are completely written, or not at all.

Related

Questions about loading files in a program

Sup!
I'm trying to understand the concepts and differences between them to load and save files, and trying to figure out the best way to load files with a program I'm making (a database). I'm using Java, but try to help me in a more general way, if possible. Whatever help I get is a lot welcome and I thank you already for it.
I know there is "File Reader" in Java and a lot of programs use files in .txt format. I tried to open a .doc file with Notepad, just to see what would happen, and NOTHING was a text. I expected that, not everything though.
I just don't know how to make a file that is not a .txt. How that happens?
This is a big one. When I load a file, should I load all its content and let it ready for use, or should I just keep the file path and read from it as I use, using offsets and pointers starting from the file path? How should I do this either way?
In this database program of mine, some things will be images (for example), and if I were to use a .txt, I would give each image a label that the program would read and then interpret it like the label of the image, and then get the image. How could I do that?
I was sure I had more questions. If I do I ask again.
Once more, thanks in advance for the attention and help.
When it comes to the .txt bit, all "plain text" will work. Just change the file extension to .ini or .sql (if sql is installed) and change it in your filepath too.

Writing icons to PE File Formats in Java

I'm in the process of writing a read/write library for Windows PE files using Java (and eventually hope to release it as OpenSource). For now though this is for cross platform compatibility and will allow me to modify strings/version info/icons/resources etc from an Ant build system or command line/terminal etc.
The problem I have reached is that I can now embed icons in the .rsrc section from a source BMP file and have it display them but unfortunately (Even though the data is correct) the icon comes out within Windows Explorer (and CFF Explorer/ Visual Studio etc) as incorrect. It has a sort of pattern across the raster data as if the image something has be Xor'd or digitally signed.
Any ideas what i'm doing wrong when adding this data?
On a side note I'm also a bit confused as to why I need to modify the height part of the BMP header so that its marked as 2x its real size.
Thanks In Advance.
- Tim.

How to create BufferedImage from bitmap data

After going through many similar looking questions I had no way but put my own question here.
I need to display an image on swing application. The source of image is bitmap data which is retrieved from MS SQL server. I have tried the following ways
TRY 1 - I have tried creating an ImageIcon from the bytes retrieved. No results.
TRY 2 - Saved the bytes in a .png file and tried loading Using ImageIO. This works fine on my local machine but fails on test server. Both are windows machines.
TRY3 - On step 2 I tried saving in different formats than .png. It does not work at all.
Please let me know what am I missing?
NOTE : I have tried including jai jars into the Referenced Libraries also.
You should have stored a hint what format the data has in the database. If not, you can only hope that ImageIO can handle it.
There is no need to write the data to files (which is a pitfall in itself, where would you write them? Think of restricted process privileges and disk quotas). Just create an InputStream that accesses the data directly (e.g. java.io.ByteArrayInputStream), that way you can have ImageIO load directly using the stream based methods.

What is the alternatives safely keep files in android app?

my app download files from server into app. There could be lots of those file. One file is about 100 mb. I need to do something to safely keep them into my app.
Thirst i tried to encrypt files. How ever this is bad solution because to encrypt and decrypt 100 mb file (it's pdf file) take a some time. Also i need at a time to read this file so i need to decrypt and write decrypted file into some other file for reading at this time files is reachable.
Furthermore i can't keep this file in memory, because of file size. So maybe there is the way to encrypt directory in internal storage where file is saved ? Or this is not good idea as i should then encrypt every file in directory.
As my files is pdf, i could put password to int, but then how to do this ? Also i could try to check if device is rooted or not, but i think someone would find workaround.
So what would you suggest ?
Thanks
It seems like you have 3 options: to encrypt your data; to store the pdfs in a private folder; or to not store the files on-device.
1) Encrypt your data: As you've said, there are disadvantages because the pdfs are quite big and if you can't have those stored in memory, you need to write the decrypted files to file anyway before displaying them, so this doesn't really solve your problem.
2) Store the pdfs in a private folder: Alternatively you could store the pdfs in a private folder only accessible through your app. This can be done using
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
as noted here. "MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application". The only problem I see with this is if people are using rooted phones and can access your app's private folders. The only way around this (as far as I know) is to use option 3.
3) Don't store the files on device: You could download the data, or parts of it, each time. This will guarantee that people can't copy the files because they never persist on the device. You could use Google Docs to stream only portions of the document to reduce download requirements if you want. The problem with this is the huge data requirement.
I think you need to weigh up the pros and cons and decide which is best for you. I'd personally go with option 2. I don't think you'll find a solution that addresses all the problems.

Resaving A Blob File In Java

I have a web application in GWT and a complementary desktop client also written in Java (so the same solution basically applies to both). In my program users can attach files, then download them later or do whatever. These files are stored as blobs and can be in just about any format. Many of the users that use Excel and Word want to be able to open the file, make changes, then have those changes stored back in the attached file. In other words, need an inline editing of attachments.
Any ideas on how to make this happen? Should I have an 'edit' mode that keeps a file handler while the file is open, and then store that File handler? Some way keeping track of whether the file is changing, or not?
Sorry about the late response. Amol >> I have that going. I want to save directly back to a blob as if it were a filehandle. Thought that was clear in my question.
I have decided that this is almost impossible with a web application without writing some kind of client interface for each and every potential file type - word, excel, pdf, graphics, etc...

Categories