In java how do byte[] buffer get the value of file data , when we write to file using fos.write( buffer,0,len);

public class Testing1 {

    public static void  main(String arg[]) throws IOException {
        FileInputStream fis  = new FileInputStream("c:\\ddk\\dk.txt");
        FileOutputStream fos = new FileOutputStream("c:\\ddk\\ok.txt");
        byte[] buffer = new byte[1024];
        int  len;
        while((len = fis.read(buffer))> -1){
            fos.write( buffer,0,len);
        }
    }
}

Open in new window

Dk champAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
        byte[] buffer = new byte[fis.available()];

Open in new window

You don't need to do that. In some circumstances that might read the entire file into memory. Just use a decent size buffer

final static int BUF_SIZE = 1 << 10 << 2; // 4KiB
byte[] buffer = new byte[BUF_SIZE];

Open in new window

1
Dk champAuthor Commented:
No you got it wrong , i want to know that, how data of the file enter in the buffer   so it can be written        fos.write( buffer,0,len);
0
krakatoaCommented:
while((len = fis.read(buffer))> 0){

You should also check end of input by a return of -1, the end of stream marker, not checking for zero length of input.
1
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Dk champAuthor Commented:
Hello krakatoa thank for helping  . Can you please tell me  byte [] buffer getting the data of the file  or we are reading the file data in
while((len = fis.read(buffer))> -1)
0
CEHJCommented:
You should also check end of input by a return of -1, the end of stream marker, not checking for zero length of input.
Quite right - i was going to mention that but forgot ;)
Can you please tell me  byte [] buffer getting the data of the file  or we are reading the file data in
Can you please explain what you mean by that - i suspect i'm not alone in needing to know that

If you mean how does the data of the file end up in the buffer then quite simply the Java method calls out to the OS to read the file into it
2
krakatoaCommented:
I'll comment, because you asked me, but CEHJ's is of course answering your query. If you still have concerns, then try to put your point to us again.
0
Dk champAuthor Commented:
Thank you krakatoa and CEHJ, i will tell you the whole problem i am facing :

I am learning how java lib extract the zip file  ,below is the whole code  

public class UnzipFiles {

    public static void main(String[] args) {
        String zipFilePath = "c:\\ddk\\m.zip";

        String destDir = "c:\\ddk\\out";

        unzip(zipFilePath, destDir);
    }

    private static void unzip(String zipFilePath, String destDir) {
        File dir = new File(destDir);
        // create output directory if it doesn't exist
        if(!dir.exists()) dir.mkdirs();
        FileInputStream fis;
        //buffer for read and write data to file
        byte[] buffer = new byte[1024];
        try {
            fis = new FileInputStream(zipFilePath);
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry ze = zis.getNextEntry();
            while(ze != null){
                String fileName = ze.getName();
                File newFile = new File(destDir + File.separator + fileName);
                System.out.println("Unzipping to "+newFile.getAbsolutePath());
                //create directories for sub directories in zip
                new File(newFile.getParent()).mkdirs();
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                //close this ZipEntry
                zis.closeEntry();
                ze = zis.getNextEntry();
            }
            //close last ZipEntry
            zis.closeEntry();
            zis.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Open in new window


this is the code , the things i understand that from  zis.getNextEntry(); we can get the meta value like crc, size , xdostime .  But the problem is  how we are accessing  file data   of the of particular entry  and adding it for writing

i know it is lengthy but if you can help me i would really help me  ,thanks in advance, and sorry for taking your time
0
krakatoaCommented:
how we are accessing  file data

Well, 'zis' IS the input stream - so we have the situation CEHJ explained. Right?
0
Dk champAuthor Commented:
like the are 3 file in the zip ,first file  enter  using zis.getNextEntry();  in  the while loop  we get file lochead like crc, size  detail , then we set the name
now we have to get the real file data and write it to the Outputstream , i didn't understand  each time diff file enter  how buffer getting the data at particular offset.                    Is it getting through zis.getNextEntry(); or  while ((len = zis.read(buffer)) > 0)
0
krakatoaCommented:
each time diff file enter  how buffer getting the data at particular offset.

Each pointer into the ZIS obtains a new file reference, so the offset doesn't come into it  - it starts at the beginning of  each file.

(Making even more of a case as to why you should understand -1 as being the end-of-file marker. Because that's what ultimately cascades the logic to adopt a new (next) file).
0
Dk champAuthor Commented:
Thank you very much krakatoa ,now i understand . So one last thing  ZIS obtain a file reference when we enter " zis.getNextEntry(); "  line or
 "while ((len = zis.read(buffer)) > 0)""
0
krakatoaCommented:
The former.
0
CEHJCommented:
As far as the entry (from which the input comes) is concerned, files don't come into it. You get a stream from which you can read
1
Dk champAuthor Commented:
Can you please point me out on which line

This is getNextEntry() code:
public ZipEntry getNextEntry() throws IOException {
        ensureOpen();
        if (entry != null) {
            closeEntry();
        }
        crc.reset();
        inf.reset();
        if ((entry = readLOC()) == null) {
            return null;
        }
        if (entry.method == STORED) {
            remaining = entry.size;
        }
        entryEOF = false;
        return entry;
    }

Open in new window


and this is readLOC

 private ZipEntry readLOC() throws IOException {
        try {
            readFully(tmpbuf, 0, LOCHDR);
        } catch (EOFException e) {
            return null;
        }
        if (get32(tmpbuf, 0) != LOCSIG) {
            return null;
        }
        // get flag first, we need check EFS.
        flag = get16(tmpbuf, LOCFLG);
        // get the entry name and create the ZipEntry first
        int len = get16(tmpbuf, LOCNAM);
        int blen = b.length;
        if (len > blen) {
            do {
                blen = blen * 2;
            } while (len > blen);
            b = new byte[blen];
        }
        readFully(b, 0, len);
        // Force to use UTF-8 if the EFS bit is ON, even the cs is NOT UTF-8
        ZipEntry e = createZipEntry(((flag & EFS) != 0)
                                    ? zc.toStringUTF8(b, len)
                                    : zc.toString(b, len));
        // now get the remaining fields for the entry
        if ((flag & 1) == 1) {
            throw new ZipException("encrypted ZIP entry not supported");
        }
        e.method = get16(tmpbuf, LOCHOW);
        e.xdostime = get32(tmpbuf, LOCTIM);
        if ((flag & 8) == 8) {
            /* "Data Descriptor" present */
            if (e.method != DEFLATED) {
                throw new ZipException(
                        "only DEFLATED entries can have EXT descriptor");
            }
        } else {
            e.crc = get32(tmpbuf, LOCCRC);
            e.csize = get32(tmpbuf, LOCSIZ);
            e.size = get32(tmpbuf, LOCLEN);
        }
        len = get16(tmpbuf, LOCEXT);
        if (len > 0) {
            byte[] extra = new byte[len];
            readFully(extra, 0, len);
            e.setExtra0(extra,
                        e.csize == ZIP64_MAGICVAL || e.size == ZIP64_MAGICVAL);
        }
        return e;
    }

Open in new window

0
Dk champAuthor Commented:
Hello CEHJ thanks , so you are saying that file do not come in the entry only meta value comes ,so from which line are we accessing the stream please can you tell me
0
CEHJCommented:
Can you please point me out on which line
Sorry - don't understand the question ...
0
Dk champAuthor Commented:
sorry for this complicating question as i was saying  when diff  entry enter into loop each time ,how does it find that for this entry i have to give this data and for next entry this data
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dk champAuthor Commented:
Thanks for helping
0
krakatoaCommented:
Actually, my earlier comment about .available() does not hold for a ZipInputStream as it would for a stream such as FileInputStream. Although I don't quite see (I was away at the time) how you came in with a question about regular file access, and it morphed into one about Zip IO.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.