KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailrepository > filepair > ResettableFileInputStream


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.mailrepository.filepair;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 /**
27  */

28 public class ResettableFileInputStream
29     extends InputStream JavaDoc
30 {
31     protected static final int DEFAULT_BUFFER_SIZE = 1024;
32
33     protected final String JavaDoc m_filename;
34     protected int m_bufferSize;
35     protected InputStream JavaDoc m_inputStream;
36     protected long m_position;
37     protected long m_mark;
38     protected boolean m_isMarkSet;
39
40     public ResettableFileInputStream( final File JavaDoc file )
41         throws IOException JavaDoc
42     {
43         this( file.getCanonicalPath() );
44     }
45
46     public ResettableFileInputStream( final String JavaDoc filename )
47         throws IOException JavaDoc
48     {
49         this( filename, DEFAULT_BUFFER_SIZE );
50     }
51
52     public ResettableFileInputStream( final String JavaDoc filename, final int bufferSize )
53         throws IOException JavaDoc
54     {
55         m_bufferSize = bufferSize;
56         m_filename = filename;
57         m_position = 0;
58
59         m_inputStream = newStream();
60     }
61
62     public void mark( final int readLimit )
63     {
64         m_isMarkSet = true;
65         m_mark = m_position;
66         m_inputStream.mark( readLimit );
67     }
68
69     public boolean markSupported()
70     {
71         return true;
72     }
73
74     public void reset()
75         throws IOException JavaDoc
76     {
77         if( !m_isMarkSet )
78         {
79             throw new IOException JavaDoc( "Unmarked Stream" );
80         }
81         try
82         {
83             m_inputStream.reset();
84         }
85         catch( final IOException JavaDoc ioe )
86         {
87             try
88             {
89                 m_inputStream.close();
90                 m_inputStream = newStream();
91                 m_inputStream.skip( m_mark );
92                 m_position = m_mark;
93             }
94             catch( final Exception JavaDoc e )
95             {
96                 throw new IOException JavaDoc( "Cannot reset current Stream: " + e.getMessage() );
97             }
98         }
99     }
100
101     protected InputStream JavaDoc newStream()
102         throws IOException JavaDoc
103     {
104         return new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( m_filename ), m_bufferSize );
105     }
106
107     public int available()
108         throws IOException JavaDoc
109     {
110         return m_inputStream.available();
111     }
112
113     public void close() throws IOException JavaDoc
114     {
115         m_inputStream.close();
116     }
117
118     public int read() throws IOException JavaDoc
119     {
120         m_position++;
121         return m_inputStream.read();
122     }
123
124     public int read( final byte[] bytes, final int offset, final int length )
125         throws IOException JavaDoc
126     {
127         final int count = m_inputStream.read( bytes, offset, length );
128         m_position += count;
129         return count;
130     }
131
132     public long skip( final long count )
133         throws IOException JavaDoc
134     {
135         m_position += count;
136         return m_inputStream.skip( count );
137     }
138 }
139
Popular Tags