Rectangle 27 2

import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
import javax.sound.midi.*;

/**
 * This class plays sounds streaming from a URL: it does not have to preload
 * the entire sound into memory before playing it. It is a command-line
 * application with no gui. It includes code to convert ULAW and ALAW
 * audio formats to PCM so they can be played. Use the -m command-line option
 * before MIDI files.
 */

public class PlaySoundStream {
    // Create a URL from the command-line argument and pass it to the 
    // right static method depending on the presence of the -m (MIDI) option.
    public static void main(String[  ] args) throws Exception {
        if (args[0].equals("-m")) streamMidiSequence(new URL(args[1]));
        else streamSampledAudio(new URL(args[0]));

        // Exit explicitly.
        // This is needed because the audio system starts background threads.
        System.exit(0);
    }

    /** Read sampled audio data from the specified URL and play it */
    public static void streamSampledAudio(URL url)
        throws IOException, UnsupportedAudioFileException,
               LineUnavailableException
    {
        AudioInputStream ain = null;  // We read audio data from here
        SourceDataLine line = null;   // And write it here.

        try {
            // Get an audio input stream from the URL
            ain=AudioSystem.getAudioInputStream(url);

            // Get information about the format of the stream
            AudioFormat format = ain.getFormat( );
            DataLine.Info info=new DataLine.Info(SourceDataLine.class,format);

            // If the format is not supported directly (i.e. if it is not PCM
            // encoded), then try to transcode it to PCM.
            if (!AudioSystem.isLineSupported(info)) {
                // This is the PCM format we want to transcode to.
                // The parameters here are audio format details that you
                // shouldn't need to understand for casual use.
                AudioFormat pcm =
                    new AudioFormat(format.getSampleRate( ), 16,
                                    format.getChannels( ), true, false);

                // Get a wrapper stream around the input stream that does the
                // transcoding for us.
                ain = AudioSystem.getAudioInputStream(pcm, ain);

                // Update the format and info variables for the transcoded data
                format = ain.getFormat( ); 
                info = new DataLine.Info(SourceDataLine.class, format);
            }

            // Open the line through which we'll play the streaming audio.
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format);  

            // Allocate a buffer for reading from the input stream and writing
            // to the line.  Make it large enough to hold 4k audio frames.
            // Note that the SourceDataLine also has its own internal buffer.
            int framesize = format.getFrameSize( );
            byte[  ] buffer = new byte[4 * 1024 * framesize]; // the buffer
            int numbytes = 0;                               // how many bytes

            // We haven't started the line yet.
            boolean started = false;

            for(;;) {  // We'll exit the loop when we reach the end of stream
                // First, read some bytes from the input stream.
                int bytesread=ain.read(buffer,numbytes,buffer.length-numbytes);
                // If there were no more bytes to read, we're done.
                if (bytesread == -1) break;
                numbytes += bytesread;

                // Now that we've got some audio data to write to the line,
                // start the line, so it will play that data as we write it.
                if (!started) {
                    line.start( );
                    started = true;
                }

                // We must write bytes to the line in an integer multiple of
                // the framesize.  So figure out how many bytes we'll write.
                int bytestowrite = (numbytes/framesize)*framesize;

                // Now write the bytes. The line will buffer them and play
                // them. This call will block until all bytes are written.
                line.write(buffer, 0, bytestowrite);

                // If we didn't have an integer multiple of the frame size, 
                // then copy the remaining bytes to the start of the buffer.
                int remaining = numbytes - bytestowrite;
                if (remaining > 0)
                    System.arraycopy(buffer,bytestowrite,buffer,0,remaining);
                numbytes = remaining;
            }

            // Now block until all buffered sound finishes playing.
            line.drain( );
        }
        finally { // Always relinquish the resources we use
            if (line != null) line.close( );
            if (ain != null) ain.close( );
        }
    }

    // A MIDI protocol constant that isn't defined by javax.sound.midi
    public static final int END_OF_TRACK = 47;

    /* MIDI or RMF data from the specified URL and play it */
    public static void streamMidiSequence(URL url)
        throws IOException, InvalidMidiDataException, MidiUnavailableException
    {
        Sequencer sequencer=null;     // Converts a Sequence to MIDI events
        Synthesizer synthesizer=null; // Plays notes in response to MIDI events

        try {
            // Create, open, and connect a Sequencer and Synthesizer
            // They are closed in the finally block at the end of this method.
            sequencer = MidiSystem.getSequencer( );
            sequencer.open( );  
            synthesizer = MidiSystem.getSynthesizer( );
            synthesizer.open( );
            sequencer.getTransmitter( ).setReceiver(synthesizer.getReceiver( ));

            // Specify the InputStream to stream the sequence from
            sequencer.setSequence(url.openStream( ));  

            // This is an arbitrary object used with wait and notify to 
            // prevent the method from returning before the music finishes
            final Object lock = new Object( );

            // Register a listener to make the method exit when the stream is 
            // done. See Object.wait( ) and Object.notify( )
            sequencer.addMetaEventListener(new MetaEventListener( ) {
                    public void meta(MetaMessage e) {
                        if (e.getType( ) == END_OF_TRACK) {
                            synchronized(lock) { 
                                lock.notify( );
                            }
                        }
                    }
                });

            // Start playing the music
            sequencer.start( );

            // Now block until the listener above notifies us that we're done.
            synchronized(lock) {
                while(sequencer.isRunning( )) {
                    try { lock.wait( ); } catch(InterruptedException e) {  }
                }
            }
        }
        finally {
            // Always relinquish the sequencer, so others can use it.
            if (sequencer != null) sequencer.close( );
            if (synthesizer != null) synthesizer.close( );
        }
    }
}

I have used this piece of code in one of my projects that deal with Audio streaming and was working just fine.

Furthermore, you can see similar examples here: Java Audio Example

This is about what I currently have implemented plus the addition of MP3 and OGG streaming thanks to JLayer, but neither mine nor yours is able to play radio streams.

java - How to properly detect, decode and play a radio stream? - Stack...

java audio stream jlayer
Rectangle 27 1

Create multiple ByteArrayInputStream instances around the returned arrays and use them in a stream that provides for concatenation. You could for instance use SequenceInputStream for this.

Enumeration<ByteArrayInputStream>
ByteReader

EDIT: I've implemented this answer, but it is probably better to create your own InputStream instance instead. Unfortunately, this solution does not let you handle IOException gracefully.

final Enumeration<ByteArrayInputStream> basEnum = new Enumeration<ByteArrayInputStream>() {

    ByteArrayInputStream baos;
    boolean ended;

    @Override
    public boolean hasMoreElements() {
        if (ended) {
            return false;
        }

        if (baos == null) {
            getNextBA();
            if (ended) {
                return false;
            }
        }

        return true;
    }

    @Override
    public ByteArrayInputStream nextElement() {
        if (ended) {
            throw new NoSuchElementException();
        }
        if (baos.available() != 0) {
            return baos;
        }

        getNextBA();
        return baos;
    }

    private void getNextBA() {

        byte[] next;
        try {
            next = byteReader.read();
        } catch (IOException e) {
            throw new IllegalStateException("Issues reading byte arrays");
        }
        if (next == null) {
            ended = true;
            return;
        }
        this.baos = new ByteArrayInputStream(next);
    }
};

SequenceInputStream sis = new SequenceInputStream(basEnum);

I am aware of the SequenceInputStream but using a ByteArrayInputStream would be neither efficient, nor synchronous. What is needed is invoking the ByteReader read() method only if there are not enough byte retrieved and buffered already to satisfy the demand of the InputStream read(...) methods.

new BufferedInputStream(new ByteArrayInputStream(...))

Thanks and +1 for the implementation, but it is not efficient. Imagine having to call ByteReader.read() thousands or millions of times on a big file...

What do you mean, not efficient? You supplied us with a single interface, which I used. It does not create any additional buffer. The only thing it creates is additional objects, which are small and can be garbage collected very easily. You ask us for help, but you think you have enough knowledge to immediately judge any submissions.

Converting a Java byte reader to an InputStream - Stack Overflow

java byte bytearray inputstream
Rectangle 27 0

Look up cipher streams in Java. You can use them to encrypt/decrypt streams on the fly so you don't have to store the whole thing in memory. All you have to do is copy the regular FileInputStream for your source file to the CipherOutputStream that's wrapping your FileOutputStream for the encrypted sink file. IOUtils even conveniently contains a copy(InputStream, OutputStream) method to do this copy for you.

public static void main(String[] args) {
    encryptFile("exampleInput.txt", "exampleOutput.txt");
}

public static void encryptFile(String source, String sink) {
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(source);
        CipherOutputStream cos = null;
        try {
            cos = new CipherOutputStream(new FileOutputStream(sink), getEncryptionCipher());
            IOUtils.copy(fis, cos);
        } finally {
            if (cos != null)
                cos.close();
        }
    } finally {
        if (fis != null)
            fis.close();
    }
}

private static Cipher getEncryptionCipher() {
    // Create AES cipher with whatever padding and other properties you want
    Cipher cipher = ... ;
    // Create AES secret key
    Key key = ... ;
    cipher.init(Cipher.ENCRYPT_MODE, key);
}

If you need to know the number of bytes that were copied, you can use IOUtils.copyLarge instead of IOUtils.copy if the file sizes exceed Integer.MAX_VALUE bytes (2 GB).

CipherInputStream
CipherOutputStream
Cipher
Cipher.DECRYPT_MODE

Take a look here for more info on cipher streams in Java.

This will save you space because you won't need to store byte arrays of your own anymore. The only stored byte[] in this system is the internal byte[] of the Cipher, which will get cleared each time enough input is entered and an encrypted block is returned by Cipher.update, or on Cipher.doFinal when the CipherOutputStream is closed. However, you don't have to worry about any of this since it's all internal and everything is managed for you.

Edit: note that this can result in certain encryption exceptions being ignored, particularly BadPaddingException and IllegalBlockSizeException. This behavior can be found in the CipherOutputStream source code. (Granted, this source is from the OpenJDK, but it probably does the same thing in the Sun JDK.) Also, from the CipherOutputStream javadocs:

java.io.OutputStream
java.io.FilterOutputStream

Moreover, this class catches all exceptions that are not thrown by its ancestor classes.

The bolded line here implies that the cryptographic exceptions are ignored, which they are. This may cause some unexpected behavior while trying to read an encrypted file, especially for block and/or padding encryption algorithms like AES. Make a mental note of this that you will get zero or partial output for the encrypted (or decrypted for CipherInputStream) file.

The Java cipher streams may shove exceptions, especially BadPaddingExceptions under the table. You've been warned.

java - Read a file to multiple byte arrays - Stack Overflow

java file file-io
Rectangle 27 0

There is already a class in Java for handling HTTP requests and responses. You should use that instead of trying to parse the response on your own. Parsing HTTP response is more difficult than you think as there are different encoding methods that you have to deal with. It isn't really raw binary data in the response payload. The HttpURLConnection class will parse headers for you and give you InputStream for the payload.

I'm writing my own because, in part of the application, I need to disregard the http.proxyHost setting that is being used in another part.

I'd fork an existing implementation rather than starting from scratch, if you cannot find a configuration parameter to do what you need. You shouldn't have any licensing issues doing this with Apache Commons HttpClient as mentioned in another answer.

Actually, I just noticed that there is a way to force URLConnections to use no proxy. I guess that'll work.

Read from InputStream in multiple formats?

java - Read from InputStream in multiple formats - Stack Overflow

java http inputstream bufferedreader
Rectangle 27 0

  • write your own InputStream wrapper where you create a temporary file to mirror the original stream content
  • dump everything read from the original input stream into this temporary file
  • when the stream was completely read you will have all the data mirrored in the temporary file
  • from now on you will loose the reference of the original stream(can be collected)
  • add a new method release() which will remove the temporary file and release any open stream.
  • you can even call release() from finalize to be sure the temporary file is release in case you forget to call release()(most of the time you should avoid using finalize, always call a method to release object resources). see Why would you ever implement finalize()?

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

If the file is not that big, read it into a byte[] array and give POI a ByteArrayInputStream created from that array.

If the file is big, then you shouldn't care, since the OS will do the caching for you as best as it can.

[EDIT] Use Apache commons-io to read the File into a byte array in an efficient way. Do not use int read() since it reads the file byte by byte which is very slow!

If you want to do it yourself, use a File object to get the length, create the array and the a loop which reads bytes from the file. You must loop since read(byte[], int offset, int len) can read less than len bytes (and usually does).

the Read() method returns int, how do i split the bytes: little or big endian ?

read returns always 0-255 or -1. Check first for -1(end of stream) and then you can cast it safety to byte.

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

As Tom Hawtin pointed out in the comments, you should be able to use Java's built-in SequenceInputStream class to deserialize an object that is split across multiple files. This approach also allows you to deserialize an object split across any number of files, because SequenceInputStream's constructor accepts two InputStream objects, one of which could be another SequenceInputStream.

I created a small example that should work on Android, although I have only tested it on a desktop Java installation. The example works on the assumption that you have serialized an object of type Foo and split the serialized version between two files names myobj.part1 and myobj.part2.

The example is pretty self explanatory, but if you have any questions, please let me know.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.SequenceInputStream;

...

ObjectInputStream in = null;

try {
    in = new ObjectInputStream(
        new SequenceInputStream(
                new FileInputStream("myobj.part1"),
                new FileInputStream("myobj.part2")));

    Foo foo = (Foo) in.readObject();

    // Do something with the deserialized object...
    foo.doSomething();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (in != null) {
        try { 
            in.close();
        } catch (IOException e) { }
    }
}

android - Reading Java serialized object that has been split across tw...

java android serialization
Rectangle 27 0

public static void main(String[] args) throws IOException {
    BufferedInputStream inputStream = new BufferedInputStream(IOUtils.toInputStream("Foobar"));
    inputStream.mark(Integer.MAX_VALUE);
    System.out.println(IOUtils.toString(inputStream));
    inputStream.reset();
    System.out.println(IOUtils.toString(inputStream));
}

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

public class ReusableBufferedInputStream extends BufferedInputStream
{

    private int totalUse;
    private int used;

    public ReusableBufferedInputStream(InputStream in, Integer totalUse)
    {
        super(in);
        if (totalUse > 1)
        {
            super.mark(Integer.MAX_VALUE);
            this.totalUse = totalUse;
            this.used = 1;
        }
        else
        {
            this.totalUse = 1;
            this.used = 1;
        }
    }

    @Override
    public void close() throws IOException
    {
        if (used < totalUse)
        {
            super.reset();
            ++used;
        }
        else
        {
            super.close();
        }
    }
}

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

The db.parse is taking "file" and returning "document".

Nopes it does not work. It still gives the same error: "Is a directory". What baffles me in this problem is that the same function and code works if I try to read from /mnt/sdcard/Fmobile/Cache while not from the /data/data/com.example.app1/cache/ given by getFilesDir() or even from getCacheDir(). Now since this works with external storage I think it might have something to do with permission or how android handles internal storage.

java - Parse XML on Android using inputstream from Cache - Stack Overf...

java android xml parsing inputstream
Rectangle 27 0

I ended up using the following to get a document from a string. String can be obtained by downloading a file or by reading a file locally. SO it solves the problem I had. If anyone needs help in getting the contents of local file as a string let me know in comments. It is fairly simple same fro fetching data from online server.

public final static Document XMLfromString(String xml)
{
    if(xml!=null)
    {
    /* Replace the & in string with &amp (if any) */
        String xmlNew=clearRefs(xml);
        Document doc = null;
    //  Log.v(tag,"After Clearing refs::"+xmlNew);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Log.v(tag,"Trying Obtaining Document");
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlNew));
            doc = db.parse(is); 
        } catch (ParserConfigurationException e) {
            Log.e(tag,"XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            connectionStatus="No data received";
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            Log.e(tag,"I/O exeption: " + e.getMessage());
            connectionStatus="No data received";
            return null;
        }
        return doc;
    }
    else
    {
        connectionStatus="Null string Passed";
        return null;
    }
}

This works irrespective of where the file was obtained from.

java - Parse XML on Android using inputstream from Cache - Stack Overf...

java android xml parsing inputstream
Rectangle 27 0

byte[] bytes = getBytes(inputStream);
POIFSFileSystem fileSystem = new POIFSFileSystem(new ByteArrayInputStream(bytes));
private static byte[] getBytes(InputStream is) throws IOException {
    byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
int n;
baos.reset();

while ((n = is.read(buffer, 0, buffer.length)) != -1) {
      baos.write(buffer, 0, n);
    }

   return baos.toByteArray();
 }

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

public class ReusableBufferedInputStream extends BufferedInputStream
{

    private int totalUse;
    private int used;

    public ReusableBufferedInputStream(InputStream in, Integer totalUse)
    {
        super(in);
        if (totalUse > 1)
        {
            super.mark(Integer.MAX_VALUE);
            this.totalUse = totalUse;
            this.used = 1;
        }
        else
        {
            this.totalUse = 1;
            this.used = 1;
        }
    }

    @Override
    public void close() throws IOException
    {
        if (used < totalUse)
        {
            super.reset();
            ++used;
        }
        else
        {
            super.close();
        }
    }
}

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

The Javadoc is pretty clear. You can use clone only to calculate different intermediate digests using the same algorithm. You cannot use DigestInputStream to calculate different digest algorithms without reading the stream multiple times. You must use a regular InputStream and multiple MessageDigest objects; read the data once, passing each buffer to all MessageDigest objects to get multiple digests with different algorithms.

You could easily encapsulate this in your own variant of DigestInputStream, say MultipleDigestInputStream that follows the same general approach but accepts a collection of MessageDigest objects or algorithm names.

MessageDigest sha = MessageDigest.getInstance("SHA-1");
MessageDigest md5 = MessageDigest.getInstance("MD5");
InputStream input = ...;
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while((len = input.read(buffer)) >= 0)
{
    sha.update(buffer,0,len);
    md5.update(buffer,0,len);
    ...
}
byte[] shaDigest = sha.digest();
byte[] md5Digest = md5.digest();

Jim, could you show me an example, please? Also, does this mean that each digest will be keeping in memory the read bytes from the InputStream?

No, each digest will keep only its own work area. You will be reading the data stream once.

Right... I see what you mean now. I have to update the digest upon every read.

java - Calculate multiple checksums from the same InputStream using Di...

java md5 sha1 checksum gnupg
Rectangle 27 0

ByteArrayInputStream is = new ByteArrayInputStream("Hello World!!".getBytes());
    if(is.markSupported()){
        is.mark("Hello World!!".length());
    }
    System.out.println(getStreamContent(is));
    is.reset();
    System.out.println("Printed once");
    System.out.println(getStreamContent(is));

Things to note: I changed the variable type from InputStream to the instance type so I could call the methods specific to that type (mark, reset and markSupported ). That allows the stream to point back to the last marked position.

java - Reading the content from an inputstream using scanner multiple ...

java inputstream java.util.scanner
Rectangle 27 0

using it already i want a one that support multiple archieve formats

java - how to use At4J or 7-Zip-JBinding to get an InputStream of a fi...

java inputstream
Rectangle 27 0

you can decorate InputStream being passed to POIFSFileSystem with a version that when close() is called it respond with reset():

class ResetOnCloseInputStream extends InputStream {

    private final InputStream decorated;

    public ResetOnCloseInputStream(InputStream anInputStream) {
        if (!anInputStream.markSupported()) {
            throw new IllegalArgumentException("marking not supported");
        }

        anInputStream.mark( 1 << 24); // magic constant: BEWARE
        decorated = anInputStream;
    }

    @Override
    public void close() throws IOException {
        decorated.reset();
    }

    @Override
    public int read() throws IOException {
        return decorated.read();
    }
}
static void closeAfterInputStreamIsConsumed(InputStream is)
        throws IOException {
    int r;

    while ((r = is.read()) != -1) {
        System.out.println(r);
    }

    is.close();
    System.out.println("=========");

}

public static void main(String[] args) throws IOException {
    InputStream is = new ByteArrayInputStream("sample".getBytes());
    ResetOnCloseInputStream decoratedIs = new ResetOnCloseInputStream(is);
    closeAfterInputStreamIsConsumed(decoratedIs);
    closeAfterInputStreamIsConsumed(decoratedIs);
    closeAfterInputStreamIsConsumed(is);
}

you can read the entire file in a byte[] (slurp mode) then passing it to a ByteArrayInputStream

How big files does it handle while using the magic constant in anInputStream.mark( 1 << 24) ?

forget about it, you can make it a parameter

What purpose serves the mark call here?

mark
reset

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

byte[] bytes = getBytes(inputStream);
POIFSFileSystem fileSystem = new POIFSFileSystem(new ByteArrayInputStream(bytes));
private static byte[] getBytes(InputStream is) throws IOException {
    byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
int n;
baos.reset();

while ((n = is.read(buffer, 0, buffer.length)) != -1) {
      baos.write(buffer, 0, n);
    }

   return baos.toByteArray();
 }

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

This answer iterates on previous ones 1|2 based on the BufferInputStream. The main changes are that it allows infinite reuse. And takes care of closing the original source input stream to free-up system resources. Your OS defines a limit on those and you don't want the program to run out of file handles (That's also why you should always 'consume' responses e.g. with the apache EntityUtils.consumeQuietly()). EDIT Updated the code to handle for gready consumers that use read(buffer, offset, length), in that case it may happen that BufferedInputStream tries hard to look at the source, this code protects against that use.

public class CachingInputStream extends BufferedInputStream {    
    public CachingInputStream(InputStream source) {
        super(new PostCloseProtection(source));
        super.mark(Integer.MAX_VALUE);
    }

    @Override
    public synchronized void close() throws IOException {
        if (!((PostCloseProtection) in).decoratedClosed) {
            in.close();
        }
        super.reset();
    }

    private static class PostCloseProtection extends InputStream {
        private volatile boolean decoratedClosed = false;
        private final InputStream source;

        public PostCloseProtection(InputStream source) {
            this.source = source;
        }

        @Override
        public int read() throws IOException {
            return decoratedClosed ? -1 : source.read();
        }

        @Override
        public int read(byte[] b) throws IOException {
            return decoratedClosed ? -1 : source.read(b);
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            return decoratedClosed ? -1 : source.read(b, off, len);
        }

        @Override
        public long skip(long n) throws IOException {
            return decoratedClosed ? 0 : source.skip(n);
        }

        @Override
        public int available() throws IOException {
            return source.available();
        }

        @Override
        public void close() throws IOException {
            decoratedClosed = true;
            source.close();
        }

        @Override
        public void mark(int readLimit) {
            source.mark(readLimit);
        }

        @Override
        public void reset() throws IOException {
            source.reset();
        }

        @Override
        public boolean markSupported() {
            return source.markSupported();
        }
    }
}

To reuse it just close it first if it wasn't.

One limitation though is that if the stream is closed before the whole content of the original stream has been read, then this decorator will have incomplete data, so make sure the whole stream is read before closing.

java - How to Cache InputStream for Multiple Use - Stack Overflow

java caching inputstream apache-poi
Rectangle 27 0

Whene you are downloading the xml file use this code:

try {
            URL   urlxml = new URL(YOUR_XML_FILE_URL);
            URLConnection ucon = urlxml.openConnection();
            ucon.setConnectTimeout(4000);
            ucon.setReadTimeout(4000);
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, 128);
            FileOutputStream fOut =this.openFileOutput("myFile.xml", Context.MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            int length = ucon.getContentLength();
            int current = 0;
            long total = 0;
            while ((current = bis.read()) != -1) {
                total += current;
                osw.write((byte) current);
            }
            osw.flush();
            osw.close();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            finishUpdate = false;
            e.printStackTrace();
        } catch (IOException e) {
            // TODO: handle exception
            finishUpdate = false;
        }

And for retreving and parsing the downloaded xml file use :

SAXParserFactory fabrique = SAXParserFactory.newInstance();
    SAXParser parseur = null;
    ArrayList temp = null;
    try {
        parseur = fabrique.newSAXParser();
        DefaultHandler handler = new ParserXMLHandler();

        File fs=context.getFilesDir();
        File file = new File(fs.getAbsoluteFile(),"Myfile.xml");
        parseur.parse(file, handler);


    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

Thanks for the answer. This would have worked if I didn't had the restriction of using same function for both files (One on internet and one stored locally). Also I cannot use SAXParser. I am using javax.xml.parsers. But your answer gave me an idea on a different problem that I had. Thanks

java - Parse XML on Android using inputstream from Cache - Stack Overf...

java android xml parsing inputstream