Source Code Cross Referenced for DocumentInputStream.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » poifs » filesystem » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » poi 3.0.2 beta2 » org.apache.poi.poifs.filesystem 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ====================================================================
002:         Licensed to the Apache Software Foundation (ASF) under one or more
003:         contributor license agreements.  See the NOTICE file distributed with
004:         this work for additional information regarding copyright ownership.
005:         The ASF licenses this file to You under the Apache License, Version 2.0
006:         (the "License"); you may not use this file except in compliance with
007:         the License.  You may obtain a copy of the License at
008:
009:         http://www.apache.org/licenses/LICENSE-2.0
010:
011:         Unless required by applicable law or agreed to in writing, software
012:         distributed under the License is distributed on an "AS IS" BASIS,
013:         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         See the License for the specific language governing permissions and
015:         limitations under the License.
016:         ==================================================================== */
017:
018:        package org.apache.poi.poifs.filesystem;
019:
020:        import java.io.*;
021:
022:        /**
023:         * This class provides methods to read a DocumentEntry managed by a
024:         * Filesystem instance.
025:         *
026:         * @author Marc Johnson (mjohnson at apache dot org)
027:         */
028:
029:        public class DocumentInputStream extends InputStream {
030:
031:            // current offset into the Document
032:            private int _current_offset;
033:
034:            // current marked offset into the Document (used by mark and
035:            // reset)
036:            private int _marked_offset;
037:
038:            // the Document's size
039:            private int _document_size;
040:
041:            // have we been closed?
042:            private boolean _closed;
043:
044:            // the actual Document
045:            private POIFSDocument _document;
046:
047:            // buffer used to read one byte at a time
048:            private byte[] _tiny_buffer;
049:
050:            // returned by read operations if we're at end of document
051:            static private final int EOD = -1;
052:
053:            /**
054:             * Create an InputStream from the specified DocumentEntry
055:             *
056:             * @param document the DocumentEntry to be read
057:             *
058:             * @exception IOException if the DocumentEntry cannot be opened
059:             *            (like, maybe it has been deleted?)
060:             */
061:
062:            public DocumentInputStream(final DocumentEntry document)
063:                    throws IOException {
064:                _current_offset = 0;
065:                _marked_offset = 0;
066:                _document_size = document.getSize();
067:                _closed = false;
068:                _tiny_buffer = null;
069:                if (document instanceof  DocumentNode) {
070:                    _document = ((DocumentNode) document).getDocument();
071:                } else {
072:                    throw new IOException(
073:                            "Cannot open internal document storage");
074:                }
075:            }
076:
077:            /**
078:             * Create an InputStream from the specified Document
079:             *
080:             * @param document the Document to be read
081:             *
082:             * @exception IOException if the DocumentEntry cannot be opened
083:             *            (like, maybe it has been deleted?)
084:             */
085:
086:            public DocumentInputStream(final POIFSDocument document)
087:                    throws IOException {
088:                _current_offset = 0;
089:                _marked_offset = 0;
090:                _document_size = document.getSize();
091:                _closed = false;
092:                _tiny_buffer = null;
093:                _document = document;
094:            }
095:
096:            /**
097:             * Returns the number of bytes that can be read (or skipped over)
098:             * from this input stream without blocking by the next caller of a
099:             * method for this input stream. The next caller might be the same
100:             * thread or or another thread.
101:             *
102:             * @return the number of bytes that can be read from this input
103:             *         stream without blocking.
104:             *
105:             * @exception IOException on error (such as the stream has been
106:             *            closed)
107:             */
108:
109:            public int available() throws IOException {
110:                dieIfClosed();
111:                return _document_size - _current_offset;
112:            }
113:
114:            /**
115:             * Closes this input stream and releases any system resources
116:             * associated with the stream.
117:             *
118:             * @exception IOException
119:             */
120:
121:            public void close() throws IOException {
122:                _closed = true;
123:            }
124:
125:            /**
126:             * Marks the current position in this input stream. A subsequent
127:             * call to the reset method repositions this stream at the last
128:             * marked position so that subsequent reads re-read the same
129:             * bytes.
130:             * <p>
131:             * The readlimit arguments tells this input stream to allow that
132:             * many bytes to be read before the mark position gets
133:             * invalidated. This implementation, however, does not care.
134:             * <p>
135:             * The general contract of mark is that, if the method
136:             * markSupported returns true, the stream somehow remembers all
137:             * the bytes read after the call to mark and stands ready to
138:             * supply those same bytes again if and whenever the method reset
139:             * is called. However, the stream is not required to remember any
140:             * data at all if more than readlimit bytes are read from the
141:             * stream before reset is called. But this stream will.
142:             *
143:             * @param ignoredReadlimit the maximum limit of bytes that can be
144:             *                         read before the mark position becomes
145:             *                         invalid. Ignored by this
146:             *                         implementation.
147:             */
148:
149:            public void mark(int ignoredReadlimit) {
150:                _marked_offset = _current_offset;
151:            }
152:
153:            /**
154:             * Tests if this input stream supports the mark and reset methods.
155:             *
156:             * @return true
157:             */
158:
159:            public boolean markSupported() {
160:                return true;
161:            }
162:
163:            /**
164:             * Reads the next byte of data from the input stream. The value
165:             * byte is returned as an int in the range 0 to 255. If no byte is
166:             * available because the end of the stream has been reached, the
167:             * value -1 is returned. The definition of this method in
168:             * java.io.InputStream allows this method to block, but it won't.
169:             *
170:             * @return the next byte of data, or -1 if the end of the stream
171:             *         is reached.
172:             *
173:             * @exception IOException
174:             */
175:
176:            public int read() throws IOException {
177:                dieIfClosed();
178:                if (atEOD()) {
179:                    return EOD;
180:                }
181:                if (_tiny_buffer == null) {
182:                    _tiny_buffer = new byte[1];
183:                }
184:                _document.read(_tiny_buffer, _current_offset++);
185:                return ((int) _tiny_buffer[0]) & 0x000000FF;
186:            }
187:
188:            /**
189:             * Reads some number of bytes from the input stream and stores
190:             * them into the buffer array b. The number of bytes actually read
191:             * is returned as an integer. The definition of this method in
192:             * java.io.InputStream allows this method to block, but it won't.
193:             * <p>
194:             * If b is null, a NullPointerException is thrown. If the length
195:             * of b is zero, then no bytes are read and 0 is returned;
196:             * otherwise, there is an attempt to read at least one byte. If no
197:             * byte is available because the stream is at end of file, the
198:             * value -1 is returned; otherwise, at least one byte is read and
199:             * stored into b.
200:             * <p>
201:             * The first byte read is stored into element b[0], the next one
202:             * into b[1], and so on. The number of bytes read is, at most,
203:             * equal to the length of b. Let k be the number of bytes actually
204:             * read; these bytes will be stored in elements b[0] through
205:             * b[k-1], leaving elements b[k] through b[b.length-1] unaffected.
206:             * <p>
207:             * If the first byte cannot be read for any reason other than end
208:             * of file, then an IOException is thrown. In particular, an
209:             * IOException is thrown if the input stream has been closed.
210:             * <p>
211:             * The read(b) method for class InputStream has the same effect as:
212:             * <p>
213:             * <code>read(b, 0, b.length)</code>
214:             *
215:             * @param b the buffer into which the data is read.
216:             *
217:             * @return the total number of bytes read into the buffer, or -1
218:             *         if there is no more data because the end of the stream
219:             *         has been reached.
220:             *
221:             * @exception IOException
222:             * @exception NullPointerException
223:             */
224:
225:            public int read(final byte[] b) throws IOException,
226:                    NullPointerException {
227:                return read(b, 0, b.length);
228:            }
229:
230:            /**
231:             * Reads up to len bytes of data from the input stream into an
232:             * array of bytes. An attempt is made to read as many as len
233:             * bytes, but a smaller number may be read, possibly zero. The
234:             * number of bytes actually read is returned as an integer.
235:             * <p>
236:             * The definition of this method in java.io.InputStream allows it
237:             * to block, but it won't.
238:             * <p>
239:             * If b is null, a NullPointerException is thrown.
240:             * <p>
241:             * If off is negative, or len is negative, or off+len is greater
242:             * than the length of the array b, then an
243:             * IndexOutOfBoundsException is thrown.
244:             * <p>
245:             * If len is zero, then no bytes are read and 0 is returned;
246:             * otherwise, there is an attempt to read at least one byte. If no
247:             * byte is available because the stream is at end of file, the
248:             * value -1 is returned; otherwise, at least one byte is read and
249:             * stored into b.
250:             * <p>
251:             * The first byte read is stored into element b[off], the next one
252:             * into b[off+1], and so on. The number of bytes read is, at most,
253:             * equal to len. Let k be the number of bytes actually read; these
254:             * bytes will be stored in elements b[off] through b[off+k-1],
255:             * leaving elements b[off+k] through b[off+len-1] unaffected.
256:             * <p>
257:             * In every case, elements b[0] through b[off] and elements
258:             * b[off+len] through b[b.length-1] are unaffected.
259:             * <p>
260:             * If the first byte cannot be read for any reason other than end
261:             * of file, then an IOException is thrown. In particular, an
262:             * IOException is thrown if the input stream has been closed.
263:             *
264:             * @param b the buffer into which the data is read.
265:             * @param off the start offset in array b at which the data is
266:             *            written.
267:             * @param len the maximum number of bytes to read.
268:             *
269:             * @return the total number of bytes read into the buffer, or -1
270:             *         if there is no more data because the end of the stream
271:             *         has been reached.
272:             *
273:             * @exception IOException
274:             * @exception NullPointerException
275:             * @exception IndexOutOfBoundsException
276:             */
277:
278:            public int read(final byte[] b, final int off, final int len)
279:                    throws IOException, NullPointerException,
280:                    IndexOutOfBoundsException {
281:                dieIfClosed();
282:                if (b == null) {
283:                    throw new NullPointerException("buffer is null");
284:                }
285:                if ((off < 0) || (len < 0) || (b.length < (off + len))) {
286:                    throw new IndexOutOfBoundsException(
287:                            "can't read past buffer boundaries");
288:                }
289:                if (len == 0) {
290:                    return 0;
291:                }
292:                if (atEOD()) {
293:                    return EOD;
294:                }
295:                int limit = Math.min(available(), len);
296:
297:                if ((off == 0) && (limit == b.length)) {
298:                    _document.read(b, _current_offset);
299:                } else {
300:                    byte[] buffer = new byte[limit];
301:
302:                    _document.read(buffer, _current_offset);
303:                    System.arraycopy(buffer, 0, b, off, limit);
304:                }
305:                _current_offset += limit;
306:                return limit;
307:            }
308:
309:            /**
310:             * Repositions this stream to the position at the time the mark
311:             * method was last called on this input stream.
312:             * <p>
313:             * The general contract of reset is:
314:             * <p>
315:             * <ul>
316:             *    <li>
317:             *        If the method markSupported returns true, then:
318:             *        <ul>
319:             *            <li>
320:             *                If the method mark has not been called since the
321:             *                stream was created, or the number of bytes read
322:             *                from the stream since mark was last called is
323:             *                larger than the argument to mark at that last
324:             *                call, then an IOException might be thrown.
325:             *            </li>
326:             *            <li>
327:             *                If such an IOException is not thrown, then the
328:             *                stream is reset to a state such that all the
329:             *                bytes read since the most recent call to mark
330:             *                (or since the start of the file, if mark has not
331:             *                been called) will be resupplied to subsequent
332:             *                callers of the read method, followed by any
333:             *                bytes that otherwise would have been the next
334:             *                input data as of the time of the call to reset.
335:             *             </li>
336:             *         </ul>
337:             *     </li>
338:             *     <li>
339:             *         If the method markSupported returns false, then:
340:             *         <ul>
341:             *             <li>
342:             *                 The call to reset may throw an IOException.
343:             *             </li>
344:             *             <li>
345:             *                 If an IOException is not thrown, then the
346:             *                 stream is reset to a fixed state that depends
347:             *                 on the particular type of the input and how it
348:             *                 was created. The bytes that will be supplied to
349:             *                 subsequent callers of the read method depend on
350:             *                 the particular type of the input stream.
351:             *             </li>
352:             *         </ul>
353:             *     </li>
354:             * </ul>
355:             * <p>
356:             * All well and good ... this class's markSupported method returns
357:             * true and this method does not care whether you've called mark
358:             * at all, or whether you've exceeded the number of bytes
359:             * specified in the last call to mark. We're basically walking a
360:             * byte array ... mark and reset to your heart's content.
361:             */
362:
363:            public void reset() {
364:                _current_offset = _marked_offset;
365:            }
366:
367:            /**
368:             * Skips over and discards n bytes of data from this input
369:             * stream. The skip method may, for a variety of reasons, end up
370:             * skipping over some smaller number of bytes, possibly 0. This
371:             * may result from any of a number of conditions; reaching end of
372:             * file before n bytes have been skipped is only one
373:             * possibility. The actual number of bytes skipped is returned. If
374:             * n is negative, no bytes are skipped.
375:             *
376:             * @param n the number of bytes to be skipped.
377:             *
378:             * @return the actual number of bytes skipped.
379:             *
380:             * @exception IOException
381:             */
382:
383:            public long skip(final long n) throws IOException {
384:                dieIfClosed();
385:                if (n < 0) {
386:                    return 0;
387:                }
388:                int new_offset = _current_offset + (int) n;
389:
390:                if (new_offset < _current_offset) {
391:
392:                    // wrap around in converting a VERY large long to an int
393:                    new_offset = _document_size;
394:                } else if (new_offset > _document_size) {
395:                    new_offset = _document_size;
396:                }
397:                long rval = new_offset - _current_offset;
398:
399:                _current_offset = new_offset;
400:                return rval;
401:            }
402:
403:            private void dieIfClosed() throws IOException {
404:                if (_closed) {
405:                    throw new IOException(
406:                            "cannot perform requested operation on a closed stream");
407:                }
408:            }
409:
410:            private boolean atEOD() {
411:                return _current_offset == _document_size;
412:            }
413:        } // end public class DocumentInputStream
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.