View | Details | Raw Unified | Return to bug 44366
Collapse All | Expand All

(-)src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java (-15 / +33 lines)
Lines 74-96 Link Here
74
    }
74
    }
75
75
76
    /**
76
    /**
77
     * Create a POIFSFileSystem from an InputStream
77
     * Create a POIFSFileSystem from an <tt>InputStream</tt>.  Normally the stream is read until
78
     * EOF.  The stream is always closed.
78
     *
79
     *
79
     * @param stream the InputStream from which to read the data
80
     * @param stream the InputStream from which to read the data
80
     *
81
     *
81
     * @exception IOException on errors reading, or on invalid data
82
     * @exception IOException on errors reading, or on invalid data
82
     */
83
     */
83
84
84
    public POIFSFileSystem(final InputStream stream)
85
    public POIFSFileSystem(InputStream stream)
85
        throws IOException
86
        throws IOException
86
    {
87
    {
87
        this();
88
        this();
89
        boolean success = false;
88
90
89
        // read the header block from the stream
91
        // read the header block from the stream
90
        HeaderBlockReader header_block_reader = new HeaderBlockReader(stream);
92
        HeaderBlockReader header_block_reader;
91
92
        // read the rest of the stream into blocks
93
        // read the rest of the stream into blocks
93
        RawDataBlockList  data_blocks         = new RawDataBlockList(stream);
94
        RawDataBlockList data_blocks;
95
        try {
96
            header_block_reader = new HeaderBlockReader(stream);
97
            data_blocks = new RawDataBlockList(stream);
98
            success = true;
99
        } finally {
100
            try {
101
                stream.close();
102
            } catch (IOException e) {
103
                if(success) {
104
                    throw new RuntimeException(e);
105
                }
106
                // else not success? Try block did not complete normally 
107
                // just print stack trace and leave original ex to be thrown
108
                e.printStackTrace();
109
            }
110
        }
111
        
94
112
95
        // set up the block allocation table (necessary for the
113
        // set up the block allocation table (necessary for the
96
        // data_blocks to be manageable
114
        // data_blocks to be manageable
Lines 123-145 Link Here
123
     * @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream 
141
     * @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream 
124
     */
142
     */
125
    public static boolean hasPOIFSHeader(InputStream inp) throws IOException {
143
    public static boolean hasPOIFSHeader(InputStream inp) throws IOException {
126
    	// We want to peek at the first 8 bytes 
144
        // We want to peek at the first 8 bytes 
127
    	inp.mark(8);
145
        inp.mark(8);
128
146
129
    	byte[] header = new byte[8];
147
        byte[] header = new byte[8];
130
    	IOUtils.readFully(inp, header);
148
        IOUtils.readFully(inp, header);
131
        LongField signature = new LongField(HeaderBlockConstants._signature_offset, header);
149
        LongField signature = new LongField(HeaderBlockConstants._signature_offset, header);
132
150
133
        // Wind back those 8 bytes
151
        // Wind back those 8 bytes
134
        if(inp instanceof PushbackInputStream) {
152
        if(inp instanceof PushbackInputStream) {
135
        	PushbackInputStream pin = (PushbackInputStream)inp;
153
            PushbackInputStream pin = (PushbackInputStream)inp;
136
        	pin.unread(header);
154
            pin.unread(header);
137
        } else {
155
        } else {
138
        	inp.reset();
156
            inp.reset();
139
        }
157
        }
140
    	
158
        
141
    	// Did it match the signature?
159
        // Did it match the signature?
142
    	return (signature.get() == HeaderBlockConstants._signature);
160
        return (signature.get() == HeaderBlockConstants._signature);
143
    }
161
    }
144
162
145
    /**
163
    /**
(-)src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (-7 / +6 lines)
Lines 15-26 Link Here
15
   limitations under the License.
15
   limitations under the License.
16
==================================================================== */
16
==================================================================== */
17
17
18
19
/*
20
 * HSSFWorkbook.java
21
 *
22
 * Created on September 30, 2001, 3:37 PM
23
 */
24
package org.apache.poi.hssf.usermodel;
18
package org.apache.poi.hssf.usermodel;
25
19
26
import org.apache.poi.POIDocument;
20
import org.apache.poi.POIDocument;
Lines 236-242 Link Here
236
        }
230
        }
237
    }
231
    }
238
232
239
     public HSSFWorkbook(InputStream s) throws IOException {
233
    /**
234
     * Constructs a <tt>HSSFWorkbook</tt> from an <tt>InputStream</tt>.<br/>
235
     * The stream is always closed.
236
     * @throws IOException upon any error while reading the stream.
237
     */
238
    public HSSFWorkbook(InputStream s) throws IOException {
240
         this(s,true);
239
         this(s,true);
241
     }
240
     }
242
241
(-)src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java (-3 / +7 lines)
Lines 20-25 Link Here
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.io.ByteArrayInputStream;
21
import java.io.ByteArrayInputStream;
22
import java.io.ByteArrayOutputStream;
22
import java.io.ByteArrayOutputStream;
23
import java.util.Arrays;
23
24
24
import junit.framework.TestCase;
25
import junit.framework.TestCase;
25
26
Lines 157-168 Link Here
157
158
158
        DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry("Empty");
159
        DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry("Empty");
159
        assertEquals("Expected zero size", 0, entry.getSize());
160
        assertEquals("Expected zero size", 0, entry.getSize());
161
        byte[] actualReadbackData;
162
        actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry));
160
        assertEquals("Expected zero read from stream", 0,
163
        assertEquals("Expected zero read from stream", 0,
161
                     IOUtils.toByteArray(new DocumentInputStream(entry)).length);
164
                     actualReadbackData.length);
162
165
163
        entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty");
166
        entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty");
167
        actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry));
164
        assertEquals("Expected size was wrong", testData.length, entry.getSize());
168
        assertEquals("Expected size was wrong", testData.length, entry.getSize());
165
        assertEquals("Expected different data read from stream", testData,
169
        assertTrue("Expected different data read from stream", 
166
                     IOUtils.toByteArray(new DocumentInputStream(entry)));
170
                Arrays.equals(testData, actualReadbackData));
167
    }
171
    }
168
}
172
}
(-)src/testcases/org/apache/poi/hssf/HSSFTests.java (-5 / +5 lines)
Lines 1-4 Link Here
1
2
/* ====================================================================
1
/* ====================================================================
3
   Licensed to the Apache Software Foundation (ASF) under one or more
2
   Licensed to the Apache Software Foundation (ASF) under one or more
4
   contributor license agreements.  See the NOTICE file distributed with
3
   contributor license agreements.  See the NOTICE file distributed with
Lines 107-118 Link Here
107
import org.apache.poi.hssf.util.TestSheetReferences;
106
import org.apache.poi.hssf.util.TestSheetReferences;
108
107
109
/**
108
/**
110
 * Test Suite for running just HSSF tests.  Mostly
109
 * Test Suite for all sub-packages of org.apache.poi.hssf<br/>
111
 * this is for my convienience.
112
 * 
110
 * 
111
 * Mostly this is for my convenience.
112
 * 
113
 * @author Andrew C. Oliver acoliver@apache.org
113
 * @author Andrew C. Oliver acoliver@apache.org
114
 */
114
 */
115
public class HSSFTests
115
public final class HSSFTests
116
{
116
{
117
117
118
    public static void main(String[] args)
118
    public static void main(String[] args)
Lines 123-129 Link Here
123
    public static Test suite()
123
    public static Test suite()
124
    {
124
    {
125
        TestSuite suite =
125
        TestSuite suite =
126
            new TestSuite("Test for org.apache.poi.hssf.usermodel");
126
            new TestSuite("Tests for org.apache.poi.hssf");
127
        //$JUnit-BEGIN$
127
        //$JUnit-BEGIN$
128
128
129
    suite.addTest(new TestSuite(TestBugs.class));
129
    suite.addTest(new TestSuite(TestBugs.class));
(-)build.xml (-2 / +1 lines)
Lines 523-530 Link Here
523
      <batchtest todir="${main.reports.test}">
523
      <batchtest todir="${main.reports.test}">
524
        <fileset dir="${main.src.test}">
524
        <fileset dir="${main.src.test}">
525
          <include name="**/Test*.java"/>
525
          <include name="**/Test*.java"/>
526
          <exclude name="**/AllTests.java"/>
526
          <exclude name="**/All*Tests.java"/>
527
          <exclude name="**/TestEmptyDocument.java"/>
528
          <exclude name="**/TestUnfixedBugs.java"/>
527
          <exclude name="**/TestUnfixedBugs.java"/>
529
          <exclude name="**/TestcaseRecordInputStream.java"/>
528
          <exclude name="**/TestcaseRecordInputStream.java"/>
530
        </fileset>
529
        </fileset>

Return to bug 44366