I have to use java.nio to create a file of any desired size by populating it with data. I am reading through a document, but am confused about when I need to flip, put, or write and am getting errors. I have successfully done this program using .io but I am testing to see if .nio will make it run faster.
This is my code so far. args[0] is the size of the file you want to make and args[1] is the name of the file to be written to
public static void main(String[] args) throws IOException
{
nioOutput fp = new nioOutput();
FileOutputStream fos = new FileOutputStream(args[1]);
FileChannel fc = fos.getChannel();
long sizeOfFile = fp.getFileSize(args[1]);
long desiredSizeOfFile = Long.parseLong(args[0]) * 1073741824; //1 Gigabyte = 1073741824 bytes
int byteLength = 1024;
ByteBuffer b = ByteBuffer.allocate(byteLength);
while(sizeOfFile + byteLength < desiredSizeOfFile)
{
// b.put((byte) byteLength);
b.flip();
fc.write(b);
sizeOfFile += byteLength;
}
int diff = (int) (desiredSizeOfFile - sizeOfFile);
sizeOfFile += diff;
fc.write(b, 0, diff);
fos.close();
System.out.println("Finished at " + sizeOfFile / 1073741824 + " Gigabyte(s)");
}
long getFileSize(String fileName)
{
File file = new File(fileName);
if (!file.exists() || !file.isFile())
{
System.out.println("File does not exist");
return -1;
}
return file.length();
}
If all you want to do is pre-extend a file to a given length with nulls, you can do it in three lines and save all that I/O:
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(desiredSizeOfFile);
raf.close();
This will operate several gazzilion times as quickly as what you are trying to do now.
sorry everyone, I figured it out.
while(sizeOfFile + byteLength < desiredSizeOfFile)
{
fc.write(b);
b.rewind();
sizeOfFile += byteLength;
}
int diff = (int) (desiredSizeOfFile - sizeOfFile);
sizeOfFile += diff;
ByteBuffer d = ByteBuffer.allocate(diff);
fc.write(d);
b.rewind();
fos.close();
System.out.println("Finished at " + sizeOfFile / 1073741824 + " Gigabyte(s)");
}
Related
I am writing a file upload Java app, and my issue is that my buffered writer is writing files short of their complete file size. When I record the bytes written, new file size vs old file size - there is always a difference - my buffered reader stops short based on the buffer size.
So when my buffer is set to 1024 for example - it only writes increments of 1024, but not the remaining bytes (the last few bytes that are less than 1024 bytes). DOCX (all X Office Files) are picky about their file size and when you write them short, they get flagged in office as corrupted.
int originalSize = (int) file.length();
FileInputStream fis = null;
BufferedOutputStream bout = null;
BufferedInputStream bin = null;
FileOutputStream fout = null;
try {
fis = new FileInputStream(file);
fout = new FileOutputStream(newfile);
bout = new BufferedOutputStream(fout);
bin = new BufferedInputStream(fis);
log.info("Setting buffer to 1024");
int total = 0;
byte buf[] = new byte[1024]; // Buffer Size (works when set to 1)
while((bin.read(buf)) != -1) {
Float size = (float) newfile.length();
bout.write(buf);
total += buf.length;
log.info("LOOP: Current Size:" + total + " New File Size: " + size + " Original Size: " + originalSize);
}
int size = (int) newfile.length();
log.info("new file size bytes: " + size);
log.info("original file size bytes: " + originalSize);
log.info("File created: " + newfile.getName());
return newfile.getName();
} catch (Exception e) {
log.error("ERROR: during input output file streaming: " + e.getMessage());
} finally {
fis.close();
bout.close();
bin.close();
}
So this works when I set the Buffer Size to 1 byte because it will completely write out the file
Any suggestions how I can get a buffered writer like this (I have to buffer it / use streams because it handles massive files) to write out the exact ammount of bytes for that file, It would be much appreciated.
Thanks
The read() method returns the number of bytes read. The buffer isn't always fully filled.
You should write something like this:
int bytesRead = 0;
while((bytesRead = bin.read(buf)) != -1) {
bout.write(buf, 0, bytesRead); // Write only the bytes read
total += bytesRead;
// ...
}
I made an app to calculate file transporting time.
Here is my code:
for (File srcfile : fList) {
srcFileName = srcfile.getName();
sizeInMB = (srcfile.length() / (1024*1024));
srcFilePath = srcPath + "/" + srcFileName;
try
{
fis = new FileInputStream(srcFilePath);
tmp = fis.available();
bufDynmc = new byte[tmp];
while (true)
{
readStartTime = System.currentTimeMillis();
bytesRead = fis.read(bufDynmc);
readEndTime = System.currentTimeMillis();
readTimeSum += (readEndTime - readStartTime);
if (bytesRead > 0)
{
if(rsltCode == RESULT_OK)
{
treeUri = rsltData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
DocumentFile newFile = pickedDir.createFile("image/jpg", srcFileName);
fos = getContentResolver().openOutputStream(newFile.getUri());
writeStartTime = System.currentTimeMillis();
fos.write(bufDynmc, 0, bytesRead);
writeEndTIme = System.currentTimeMillis();
writeTimeSum += (writeEndTIme - writeStartTime);
}
continue;
} else break;
}
sizeSum += sizeInMB;
fis.close(); fos.close();
} catch (Exception e) {e.printStackTrace();}
rescanGallery(treeUri);
}
What I want to measure is how much time has been spend from source directory to RAM ,
and from RAM to destination directory.
Am I measuring the reading time and writing time with right method?
I'm not sure cuz this program gives me a time which is too fast.
I want to add an image to particular position in my word document using docx4j. I don't want inline insertion. The code below performs adding the image inline with text. But I want floating insertion where I can explicitly give the location of where the image should be placed in the page. Please help me.
public R addUserPic(P parag, WordprocessingMLPackage wordMLPackage)
throws Exception {
File file = new File("src/main/resources/PictureNew.png");
byte[] bytes = convertImageToByteArray(file);
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
.createImagePart(wordMLPackage, bytes);
int docPrId = 1;
int cNvPrId = 2;
Inline inline = imagePart.createImageInline("Filename hint",
"Alternative text", docPrId, cNvPrId, false);
ObjectFactory factory = new ObjectFactory();
R run = factory.createR();
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return run;
}
private static byte[] convertImageToByteArray(File file)
throws FileNotFoundException, IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
System.out.println("Could not completely read file "
+ file.getName());
}
is.close();
return bytes;
}
The thread you have cross posted in, at http://www.docx4java.org/forums/docx-java-f6/how-to-create-a-floating-image-t1224.html answers your question.
I have an InputStream, and the relative file name and size.
I need to access/read some random (increasing) positions in the InputStream. This positions are stored in an integer array (named offsets).
InputStream inputStream = ...
String fileName = ...
int fileSize = (int) ...
int[] offsets = new int[]{...}; // the random (increasing) offsets array
Now, given an InputStream, I've found only two possible solutions to jump to random (increasing) positions of the file.
The first one is to use the skip() method of the InputStream (note that I actually use BufferedInputStream, since I will need to mark() and reset() the file pointer).
//Open a BufferInputStream:
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] bytes = new byte[1];
int curFilePointer = 0;
long numBytesSkipped = 0;
long numBytesToSkip = 0;
int numBytesRead = 0;
//Check the file size:
if ( fileSize < offsets[offsets.length-1] ) { // the last (bigger) offset is bigger then the file size...
//Debug:
Log.d(TAG, "The file is too small!\n");
return;
}
for (int i=0, k=0; i < offsets.length; i++, k=0) { // for each offset I have to jump...
try {
//Jump to the offset [i]:
while( (curFilePointer < offsets[i]) && (k < 10) ) { // until the correct offset is reached (at most 10 tries)
numBytesToSkip = offsets[i] - curFilePointer;
numBytesSkipped = bufferedInputStream.skip(numBytesToSkip);
curFilePointer += numBytesSkipped; // move the file pointer forward
//Debug:
Log.d(TAG, "FP: " + curFilePointer + "\n");
k++;
}
if ( curFilePointer != offsets[i] ) { // it did NOT jump properly... (what's going on?!)
//Debug:
Log.d(TAG, "InputStream.skip() DID NOT JUMP PROPERLY!!!\n");
break;
}
//Read the content of the file at the offset [i]:
numBytesRead = bufferedInputStream.read(bytes, 0, bytes.length);
curFilePointer += numBytesRead; // move the file pointer forward
//Debug:
Log.d(TAG, "READ [" + curFilePointer + "]: " + bytes[0] + "\n");
}
catch ( IOException e ) {
e.printStackTrace();
break;
}
catch ( IndexOutOfBoundsException e ) {
e.printStackTrace();
break;
}
}
//Close the BufferInputStream:
bufferedInputStream.close()
The problem is that, during my tests, for some (usually big) offsets, it has cycled 5 or more times before skipping the correct number of bytes. Is it normal? And, above all, can/should I thrust skip()? (That is: Are 10 cycles enough to be SURE it will ALWAYS arrive to the correct offset?)
The only alternative way I've found is the one of creating a RandomAccessFile from the InputStream, through File.createTempFile(prefix, suffix, directory) and the following function.
public static RandomAccessFile toRandomAccessFile(InputStream inputStream, File tempFile, int fileSize) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile(tempFile, "rw");
byte[] buffer = new byte[fileSize];
int numBytesRead = 0;
while ( (numBytesRead = inputStream.read(buffer)) != -1 ) {
randomAccessFile.write(buffer, 0, numBytesRead);
}
randomAccessFile.seek(0);
return randomAccessFile;
}
Having a RandomAccessFile is actually a much better solution, but the performance are exponentially worse (above all because I will have more than a single file).
EDIT: Using byte[] buffer = new byte[fileSize] speeds up (and a lot) the RandomAccessFile creation!
//Create a temporary RandomAccessFile:
File tempFile = File.createTempFile(fileName, null, context.getCacheDir());
RandomAccessFile randomAccessFile = toRandomAccessFile(inputStream, tempFile, fileSize);
byte[] bytes = new byte[1];
int numBytesRead = 0;
//Check the file size:
if ( fileSize < offsets[offsets.length-1] ) { // the last (bigger) offset is bigger then the file size...
//Debug:
Log.d(TAG, "The file is too small!\n");
return;
}
for (int i=0, k=0; i < offsets.length; i++, k=0) { // for each offset I have to jump...
try {
//Jump to the offset [i]:
randomAccessFile.seek(offsets[i]);
//Read the content of the file at the offset [i]:
numBytesRead = randomAccessFile.read(bytes, 0, bytes.length);
//Debug:
Log.d(TAG, "READ [" + (randomAccessFile.getFilePointer()-4) + "]: " + bytes[0] + "\n");
}
catch ( IOException e ) {
e.printStackTrace();
break;
}
catch ( IndexOutOfBoundsException e ) {
e.printStackTrace();
break;
}
}
//Delete the temporary RandomAccessFile:
randomAccessFile.close();
tempFile.delete();
Now, is there a better (or more elegant) solution to have a "random" access from an InputStream?
It's a bit unfortunate you have an InputStream to begin with, but in this situation buffering the stream in a file is of no use iff you are always skipping forward. But you don't have to count the number of times you have called skip, that's not really of interest.
What you do have to check if the stream has ended already, to prevent an infinite loop. Checking the source of the default skip implementation, I'd say you'll have to keep calling skip until it returns 0. This will indicate the end of stream has been reached. The JavaDoc was a bit unclear about this for my taste.
You can't. An InputStream is a stream, that is to say a sequential construct. Your question embodies a contradiction in terms.
I want to know if really 'put' has succeeded in putting the file to destination. If for any reason the file is not put in destination [maybe due to problems in destination server like space constraint, etc] I need to know that.
Code:
private static boolean putFile(String m_sLocalFile, FtpClient m_client) {
boolean success = false;
int BUFFER_SIZE = 10240;
if (m_sLocalFile.length() == 0) {
System.out.println("Please enter file name");
}
byte[] buffer = new byte[BUFFER_SIZE];
try {
File f = new File(m_sLocalFile);
int size = (int) f.length();
System.out.println("File " + m_sLocalFile + ": " + size + " bytes");
System.out.println(size);
FileInputStream in = new FileInputStream(m_sLocalFile);
OutputStream out = m_client.put(f.getName());
int counter = 0;
while (true) {
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
System.out.println(counter);
}
out.close();
in.close();
} catch (Exception ex) {
System.out.println("Error: " + ex.toString());
}
return success;
}
I would expect it to throw an IOException. Do you have any reason to believe it doesn't? But you shouldn't be using that class directly, you should be using an ftp: URL and its URLConnection class to do the I/O with, after calling setDoOutput(true).