Results 1 to 3 of 3
  1. #1
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Reusing code for a slightly different class member.

    I have a class that has an "extract" member. My goal is to use it to extract files and archives that may or may not be compressed. I'm using the compress libraries out of org.apahce.commons. The code for extracting a tar archive vs a zip archive is exactly the same except the inupt stream is a ZipArchiveInputStream vs TarArchiveInputStream and the output stream is written from a ZipArchiveEntry instance for zip files and TarArchiveEntry instance for tar files.

    Java Code:
    ZipArchiveEntry entry = null;
    ZipArchiveInputStream zis = null;
    while((entry = (ZipArchiveEntry) zis.getNextEntry())) {
    ... some code

    Java Code:
    TarArchiveEntry entry = null;
    TarArchiveInputStream  tis = null;
    while ((entry = (TarArchiveEntry) tis.getNextEntry())) {
    ...some conde
    What is the best/appropriate way to consolidate this so I can reuse the "some code"?

    Thank you.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Reusing code for a slightly different class member.

    Moved from New to Java to Apache Commons.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: Reusing code for a slightly different class member.

    That is where an aptly named method comes in:
    Java Code:
    while((entry = (ZipArchiveEntry) zis.getNextEntry())) {
    This example assumes you need "data" in that "some code" section.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. JNI - Accessing Class member from Native Code
    By vinaykutsa in forum Advanced Java
    Replies: 0
    Last Post: 05-27-2011, 09:01 AM
  2. Replies: 2
    Last Post: 02-16-2011, 12:35 PM
  3. declare class data member as an inteface type
    By everurz in forum Advanced Java
    Replies: 3
    Last Post: 12-21-2009, 02:04 PM
  4. ReUsing methods
    By random0munky in forum New To Java
    Replies: 7
    Last Post: 10-16-2009, 09:53 AM
  5. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 10:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts