RandomAccessFile in JAR

Discussion in 'Java' started by ak, Oct 24, 2003.

  1. ak

    ak Guest

    Hi, I need to create RandomAccessFile for file which is in JAR. Is it
    possible?
    ak, Oct 24, 2003
    #1
    1. Advertising

  2. "ak" <> writes:
    > Hi, I need to create RandomAccessFile for file which is in JAR. Is it
    > possible?


    No.
    Thomas Weidenfeller, Oct 24, 2003
    #2
    1. Advertising

  3. ak wrote:
    > Hi, I need to create RandomAccessFile for file which is in JAR. Is it
    > possible?


    No. The compression makes local changes impossible.
    Michael Borgwardt, Oct 24, 2003
    #3
  4. ak

    Ben_ Guest

    Hello,

    If what you want to do is modify a ZipEntry without extracting it, then I
    don't see it feasible, because you have to extract the ZipEntry to alter it
    and then re-zip it. My assumption is based on the fact that the API doesn't
    mention this and on the fact that it's what Zip file managers do (like
    WinZip, etc).
    Ben_, Oct 24, 2003
    #4
  5. ak

    ak Guest

    "Michael Borgwardt" <> schrieb im Newsbeitrag
    news:bnb0ku$v6il9$-berlin.de...
    > ak wrote:
    > > Hi, I need to create RandomAccessFile for file which is in JAR. Is it
    > > possible?

    >
    > No. The compression makes local changes impossible.
    >

    Hmm, I don't want to change anything. Just create RandomAccessFile.
    ak, Oct 24, 2003
    #5
  6. ak

    Ben_ Guest

    > Hmm, I don't want to change anything. Just create RandomAccessFile.
    The idea you have is obvious to you, but not to us. You should give details
    if you want support... :)
    Ben_, Oct 24, 2003
    #6
  7. ak

    ak Guest

    That simple, i wrote RandomAccessByteArray (which extends
    RandomAccessFile) - so I can read byte array as RandomAccessFile.
    Constructor of RandomAccessFile needs valid File.
    I used before some temporary file. But this approach dont work if my
    programm runs in applet.


    hier is my code:

    //create file in same directory with my JAR file
    private static File f = new File("..\\xxx.tmp");

    int fp;

    private static File createFile() throws IOException {
    FileOutputStream fos = new FileOutputStream(f);
    //ensure that file really created
    fos.write(100);
    IOutils.closeStream(fos);
    return f;
    }

    byte[] buf;
    String mode;
    int length;
    int offset;

    /**
    * create new RandomAccessByteArray that looks like RandomAccessFile
    * @param mode ignored
    * @param data byte array
    * @throws IOException
    */
    public RandomAccessByteArray(String mode, byte[] data) throws IOException {
    this(mode, data, 0, data.length);
    }

    /**
    * create new RandomAccessByteArray that looks like RandomAccessFile
    * @param mode ignored
    * @param data byte array
    * @param off index of the first byte
    * @param length number of bytes
    * @throws IOException
    */
    public RandomAccessByteArray(String mode, byte[] data, int off, int length)
    throws IOException {
    super(createFile(), "r");
    this.mode = mode;
    this.buf = data;
    this.length = length;
    this.offset = off;

    //clean up
    try {
    super.close();
    }
    catch(IOException ex) {
    ex.printStackTrace();
    }
    finally {
    f.delete();
    }
    }


    "Ben_" <reply@newsgroup> schrieb im Newsbeitrag
    news:3f9913ff$0$259$...
    > > Hmm, I don't want to change anything. Just create RandomAccessFile.

    > The idea you have is obvious to you, but not to us. You should give

    details
    > if you want support... :)
    >
    >
    ak, Oct 24, 2003
    #7
  8. ak wrote:
    > That simple, i wrote RandomAccessByteArray (which extends
    > RandomAccessFile) - so I can read byte array as RandomAccessFile.
    > Constructor of RandomAccessFile needs valid File.
    > I used before some temporary file. But this approach dont work if my
    > programm runs in applet.


    And you thought you could put it in the applet's own JAR???
    That's even more impossible.

    Do you actually need *random* access? Or just the readXX methods?
    In the latter case, a DataInputStream wrapped around a ByteArrayInputStream
    offers the same.
    Michael Borgwardt, Oct 24, 2003
    #8
  9. ak

    ak Guest

    Yes, I really need *random* access.


    "Michael Borgwardt" <> schrieb im Newsbeitrag
    news:bnb7gk$v74ju$-berlin.de...
    > ak wrote:
    > > That simple, i wrote RandomAccessByteArray (which extends
    > > RandomAccessFile) - so I can read byte array as RandomAccessFile.
    > > Constructor of RandomAccessFile needs valid File.
    > > I used before some temporary file. But this approach dont work if my
    > > programm runs in applet.

    >
    > And you thought you could put it in the applet's own JAR???
    > That's even more impossible.
    >
    > Do you actually need *random* access? Or just the readXX methods?
    > In the latter case, a DataInputStream wrapped around a

    ByteArrayInputStream
    > offers the same.
    ak, Oct 24, 2003
    #9
  10. ak wrote:
    > Yes, I really need *random* access.


    Then I think the best method would be to implement the methods from
    RandomAccessFile that you need yourself to work directly on a byte
    array. Shouldn't be too difficult.

    Having a temporary file is simply not possible in an applet, at least
    not with the standard security policy.
    Michael Borgwardt, Oct 24, 2003
    #10
  11. ak

    ak Guest

    The goal of RandomAccessByteArray is that there was no need to implement
    same things second time for byte arrays - just create RandomAccessByteArray
    and pass it to my standard reader which works on files.

    "Michael Borgwardt" <> schrieb im Newsbeitrag
    news:bnb8vr$um865$-berlin.de...
    > ak wrote:
    > > Yes, I really need *random* access.

    >
    > Then I think the best method would be to implement the methods from
    > RandomAccessFile that you need yourself to work directly on a byte
    > array. Shouldn't be too difficult.


    I think I should create a new Interface and use it instead of
    RandomAccessFile:

    public interface MyRandomAccess extends DataInput, DataOutput {
    //methods from RandomAccessFile
    }

    public class MyRandomAccessFile extends RandomAccessFile implements
    MyRandomAccess {
    public MyRandomAccessFile(String s, File f) {
    super(s, f)
    }
    }

    public class MyRandomAccessByteArray implements MyRandomAccess {

    }

    Thanks for all comments!

    Andrei
    ak, Oct 24, 2003
    #11
  12. ak

    Ben_ Guest

    Zip classes uses Streams to inflate/deflate zipentries. So maybe you can do
    the work in memory.
    Ben_, Oct 24, 2003
    #12
  13. ak

    Roedy Green Guest

    On Fri, 24 Oct 2003 11:20:54 +0200, "ak" <> wrote
    or quoted :

    >Hi, I need to create RandomAccessFile for file which is in JAR. Is it
    >possible?


    I think you mean you want to read elements in random order. For that
    use ZipFile and ZipEntry. We aware that if you created the file with
    ZipOutputStream the length fields will be wrong in ZipEntry.


    If you actually want to read the jar at a low level because you want
    to roll you own ZipFile, you would use RandomAccessFile. See
    http://mindprod.com/fileio.html for how.

    Most likely you just want to read:
    http://mindprod.com/jgloss/zip.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Oct 24, 2003
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher Fuhrman
    Replies:
    5
    Views:
    1,141
    Andrew Thompson
    Aug 20, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    555
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,696
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,730
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    640
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page