/** * Create an InputStream from the specified DocumentEntry * * @param document the DocumentEntry to be read * * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has * been deleted?) */ public DocumentInputStream(DocumentEntry document) { if (!(document is DocumentNode)) { throw new IOException("Cannot open internal document storage"); } DocumentNode documentNode = (DocumentNode)document; DirectoryNode parentNode = (DirectoryNode)document.Parent; if (documentNode.Document != null) { delegate1 = new ODocumentInputStream(document); } else if (parentNode.FileSystem != null) { delegate1 = new ODocumentInputStream(document); } else if (parentNode.NFileSystem != null) { delegate1 = new NDocumentInputStream(document); } else { throw new IOException("No FileSystem bound on the parent, can't read contents"); } }
/** * Copies an Entry into a target POIFS directory, recursively */ public static void CopyNodeRecursively(Entry entry, DirectoryEntry target) { // System.err.println("copyNodeRecursively called with "+entry.getName()+ // ","+target.getName()); DirectoryEntry newTarget = null; if (entry.IsDirectoryEntry) { DirectoryEntry dirEntry = (DirectoryEntry)entry; newTarget = target.CreateDirectory(entry.Name); newTarget.StorageClsid = (dirEntry.StorageClsid); IEnumerator <Entry> entries = dirEntry.Entries; while (entries.MoveNext()) { CopyNodeRecursively((Entry)entries.Current, newTarget); } } else { DocumentEntry dentry = (DocumentEntry)entry; DocumentInputStream dstream = new DocumentInputStream(dentry); target.CreateDocument(dentry.Name, dstream); dstream.Close(); } }
/** * package scoped constructor * * @param stream the DocumentInputStream, freshly opened * @param path the path of the document * @param documentName the name of the document */ public POIFSReaderEvent(DocumentInputStream stream, POIFSDocumentPath path, String documentName) { this.stream = stream; this.path = path; this.documentName = documentName; }
/** * Copies an Entry into a target POIFS directory, recursively */ public static void CopyNodeRecursively(Entry entry, DirectoryEntry target) { // System.err.println("copyNodeRecursively called with "+entry.getName()+ // ","+target.getName()); DirectoryEntry newTarget = null; if (entry.IsDirectoryEntry) { DirectoryEntry dirEntry = (DirectoryEntry)entry; newTarget = target.CreateDirectory(entry.Name); newTarget.StorageClsid=(dirEntry.StorageClsid); IEnumerator<Entry> entries = dirEntry.Entries; while (entries.MoveNext()) { CopyNodeRecursively((Entry)entries.Current, newTarget); } } else { DocumentEntry dentry = (DocumentEntry)entry; DocumentInputStream dstream = new DocumentInputStream(dentry); target.CreateDocument(dentry.Name, dstream); dstream.Close(); } }
/** * Checks to see if two Documents have the same name * and the same contents. (Their parent directories are * not checked) */ public static bool AreDocumentsIdentical(DocumentEntry docA, DocumentEntry docB) { if (!docA.Name.Equals(docB.Name)) { // Names don't match, not the same return(false); } if (docA.Size != docB.Size) { // Wrong sizes, can't have the same contents return(false); } bool matches = true; DocumentInputStream inpA = null, inpB = null; try { inpA = new DocumentInputStream(docA); inpB = new DocumentInputStream(docB); int readA, readB; do { readA = inpA.Read(); readB = inpB.Read(); if (readA != readB) { matches = false; break; } } while (readA != -1 && readB != -1); } finally { if (inpA != null) { inpA.Close(); } if (inpB != null) { inpB.Close(); } } return(matches); }
/** * Create an InputStream from the specified Document * * @param document the Document to be read */ public DocumentInputStream(NPOIFSDocument document) { delegate1 = new NDocumentInputStream(document); }
public void TestSkip() { DocumentInputStream[] streams = new DocumentInputStream[] { new DocumentInputStream(_workbook_o), new NDocumentInputStream(_workbook_n) }; foreach (DocumentInputStream stream in streams) { Assert.AreEqual(_workbook_size, stream.Available()); int count = stream.Available(); while (stream.Available() >= _buffer_size) { Assert.AreEqual(_buffer_size, stream.Skip(_buffer_size)); count -= _buffer_size; Assert.AreEqual(count, stream.Available()); } Assert.AreEqual(_workbook_size % _buffer_size, stream.Skip(_buffer_size)); Assert.AreEqual(0, stream.Available()); stream.Reset(); Assert.AreEqual(_workbook_size, stream.Available()); Assert.AreEqual(_workbook_size, stream.Skip(_workbook_size * 2)); Assert.AreEqual(0, stream.Available()); stream.Reset(); Assert.AreEqual(_workbook_size, stream.Available()); Assert.AreEqual(_workbook_size, stream.Skip(2 + (long)Int32.MaxValue)); Assert.AreEqual(0, stream.Available()); } }
public void TestComplexBufferRead() { DocumentInputStream[] streams = new DocumentInputStream[] { new DocumentInputStream(_workbook_o), new NDocumentInputStream(_workbook_n) }; foreach (DocumentInputStream stream in streams) { try { stream.Read(null, 0, 1); Assert.Fail("Should have caught NullPointerException"); } catch (ArgumentException) { // as expected } // test illegal offsets and lengths try { stream.Read(new byte[5], -4, 0); Assert.Fail("Should have caught IndexOutOfBoundsException"); } catch (IndexOutOfRangeException) { // as expected } try { stream.Read(new byte[5], 0, -4); Assert.Fail("Should have caught IndexOutOfBoundsException"); } catch (IndexOutOfRangeException) { // as expected } try { stream.Read(new byte[5], 0, 6); Assert.Fail("Should have caught IndexOutOfBoundsException"); } catch (IndexOutOfRangeException) { // as expected } // test Reading zero Assert.AreEqual(0, stream.Read(new byte[5], 0, 0)); Assert.AreEqual(_workbook_size, stream.Available()); byte[] buffer = new byte[_workbook_size]; int offset = 0; while (stream.Available() >= _buffer_size) { Arrays.Fill(buffer, (byte)0); Assert.AreEqual(_buffer_size, stream.Read(buffer, offset, _buffer_size)); for (int j = 0; j < offset; j++) { Assert.AreEqual(0, buffer[j], "Checking byte " + j); } for (int j = offset; j < (offset + _buffer_size); j++) { Assert.AreEqual(_workbook_data[j], buffer[j], "Checking byte " + j); } for (int j = offset + _buffer_size; j < buffer.Length; j++) { Assert.AreEqual(0, buffer[j], "Checking byte " + j); } offset += _buffer_size; Assert.AreEqual(_workbook_size - offset, stream.Available(), "offset " + offset); } Assert.AreEqual(_workbook_size % _buffer_size, stream.Available()); Arrays.Fill(buffer, (byte)0); int count = stream.Read(buffer, offset, _workbook_size % _buffer_size); Assert.AreEqual(_workbook_size % _buffer_size, count); for (int j = 0; j < offset; j++) { Assert.AreEqual(0, buffer[j], "Checking byte " + j); } for (int j = offset; j < buffer.Length; j++) { Assert.AreEqual(_workbook_data[j], buffer[j], "Checking byte " + j); } Assert.AreEqual(_workbook_size, offset + count); for (int j = count; j < offset; j++) { Assert.AreEqual(0, buffer[j], "byte " + j); } Assert.AreEqual(-1, stream.Read(buffer, 0, 1)); stream.Close(); try { stream.Read(buffer, 0, 1); Assert.Fail("Should have caught IOException"); } catch (IOException) { // as expected } } }
public void TestBufferRead() { DocumentInputStream[] streams = new DocumentInputStream[] { new DocumentInputStream(_workbook_o), new NDocumentInputStream(_workbook_n) }; foreach (DocumentInputStream stream in streams) { // Need to give a byte array to read try { stream.Read(null); Assert.Fail("Should have caught NullPointerException"); } catch (NullReferenceException) { // as expected } // test Reading zero length buffer Assert.AreEqual(0, stream.Read(new byte[0])); Assert.AreEqual(_workbook_size, stream.Available()); byte[] buffer = new byte[_buffer_size]; int offset = 0; while (stream.Available() >= buffer.Length) { Assert.AreEqual(_buffer_size, stream.Read(buffer)); for (int j = 0; j < buffer.Length; j++) { Assert.AreEqual( _workbook_data[offset], buffer[j], "in main loop, byte " + offset); offset++; } Assert.AreEqual(_workbook_size - offset, stream.Available(), "offset " + offset); } Assert.AreEqual(_workbook_size % _buffer_size, stream.Available()); Arrays.Fill(buffer, (byte)0); int count = stream.Read(buffer); Assert.AreEqual(_workbook_size % _buffer_size, count); for (int j = 0; j < count; j++) { Assert.AreEqual( _workbook_data[offset], buffer[j], "past main loop, byte " + offset); offset++; } Assert.AreEqual(_workbook_size, offset); for (int j = count; j < buffer.Length; j++) { Assert.AreEqual(0, buffer[j], "Checking remainder, byte " + j); } Assert.AreEqual(-1, stream.Read(buffer)); stream.Close(); try { stream.Read(buffer); Assert.Fail("Should have caught IOException"); } catch (IOException) { // as expected } } }
public void TestReadSingleByte() { DocumentInputStream[] streams = new DocumentInputStream[] { new DocumentInputStream(_workbook_o), new NDocumentInputStream(_workbook_n) }; foreach (DocumentInputStream stream in streams) { int remaining = _workbook_size; // Try and read each byte in turn for (int j = 0; j < _workbook_size; j++) { int b = stream.Read(); Assert.IsTrue(b >= 0, "Checking sign of " + j); Assert.AreEqual(_workbook_data[j], (byte)b, "validating byte " + j); remaining--; Assert.AreEqual( remaining, stream.Available(), "Checking remaining After Reading byte " + j); } // Ensure we fell off the end Assert.AreEqual(-1, stream.Read()); // Check that After close we can no longer read stream.Close(); try { stream.Read(); Assert.Fail("Should have caught IOException"); } catch (IOException) { // as expected } } }
public void TestMarkFunctions() { byte[] buffer = new byte[_workbook_size / 5]; byte[] small_buffer = new byte[212]; DocumentInputStream[] streams = new DocumentInputStream[] { new DocumentInputStream(_workbook_o), new NDocumentInputStream(_workbook_n) }; foreach (DocumentInputStream stream in streams) { // Read a fifth of it, and check all's correct stream.Read(buffer); for (int j = 0; j < buffer.Length; j++) { Assert.AreEqual(_workbook_data[j], buffer[j], "Checking byte " + j); } Assert.AreEqual(_workbook_size - buffer.Length, stream.Available()); // Reset, and check the available goes back to being the // whole of the stream stream.Reset(); Assert.AreEqual(_workbook_size, stream.Available()); // Read part of a block stream.Read(small_buffer); for (int j = 0; j < small_buffer.Length; j++) { Assert.AreEqual(_workbook_data[j], small_buffer[j], "Checking byte " + j); } Assert.AreEqual(_workbook_size - small_buffer.Length, stream.Available()); stream.Mark(0); // Read the next part stream.Read(small_buffer); for (int j = 0; j < small_buffer.Length; j++) { Assert.AreEqual(_workbook_data[j + small_buffer.Length], small_buffer[j], "Checking byte " + j); } Assert.AreEqual(_workbook_size - 2 * small_buffer.Length, stream.Available()); // Reset, check it goes back to where it was stream.Reset(); Assert.AreEqual(_workbook_size - small_buffer.Length, stream.Available()); // Read stream.Read(small_buffer); for (int j = 0; j < small_buffer.Length; j++) { Assert.AreEqual(_workbook_data[j + small_buffer.Length], small_buffer[j], "Checking byte " + j); } Assert.AreEqual(_workbook_size - 2 * small_buffer.Length, stream.Available()); // Now read at various points Arrays.Fill(small_buffer, (byte)0); stream.Read(small_buffer, 6, 8); stream.Read(small_buffer, 100, 10); stream.Read(small_buffer, 150, 12); int pos = small_buffer.Length * 2; for (int j = 0; j < small_buffer.Length; j++) { byte exp = 0; if (j >= 6 && j < 6 + 8) { exp = _workbook_data[pos]; pos++; } if (j >= 100 && j < 100 + 10) { exp = _workbook_data[pos]; pos++; } if (j >= 150 && j < 150 + 12) { exp = _workbook_data[pos]; pos++; } Assert.AreEqual(exp, small_buffer[j], "Checking byte " + j); } } // Now repeat it with spanning multiple blocks streams = new DocumentInputStream[] { new DocumentInputStream(_workbook_o), new NDocumentInputStream(_workbook_n) }; foreach (DocumentInputStream stream in streams) { // Read several blocks work buffer = new byte[_workbook_size / 5]; stream.Read(buffer); for (int j = 0; j < buffer.Length; j++) { Assert.AreEqual(_workbook_data[j], buffer[j], "Checking byte " + j); } Assert.AreEqual(_workbook_size - buffer.Length, stream.Available()); // Read all of it again, check it began at the start again stream.Reset(); Assert.AreEqual(_workbook_size, stream.Available()); stream.Read(buffer); for (int j = 0; j < buffer.Length; j++) { Assert.AreEqual(_workbook_data[j], buffer[j], "Checking byte " + j); } // Mark our position, and read another whole buffer stream.Mark(12); stream.Read(buffer); Assert.AreEqual(_workbook_size - (2 * buffer.Length), stream.Available()); for (int j = buffer.Length; j < (2 * buffer.Length); j++) { Assert.AreEqual(_workbook_data[j], buffer[j - buffer.Length], "Checking byte " + j); } // Reset, should go back to only one buffer full read stream.Reset(); Assert.AreEqual(_workbook_size - buffer.Length, stream.Available()); // Read the buffer again stream.Read(buffer); Assert.AreEqual(_workbook_size - (2 * buffer.Length), stream.Available()); for (int j = buffer.Length; j < (2 * buffer.Length); j++) { Assert.AreEqual(_workbook_data[j], buffer[j - buffer.Length], "Checking byte " + j); } Assert.IsTrue(stream.MarkSupported()); } }
public void TestConstructor() { DocumentInputStream ostream = new DocumentInputStream(_workbook_o); DocumentInputStream nstream = new NDocumentInputStream(_workbook_n); Assert.AreEqual(_workbook_size, _workbook_o.Size); Assert.AreEqual(_workbook_size, _workbook_n.Size); Assert.AreEqual(_workbook_size, ostream.Available()); Assert.AreEqual(_workbook_size, nstream.Available()); }
/** * Checks to see if two Documents have the same name * and the same contents. (Their parent directories are * not checked) */ public static bool AreDocumentsIdentical(DocumentEntry docA, DocumentEntry docB) { if (!docA.Name.Equals(docB.Name)) { // Names don't match, not the same return false; } if (docA.Size != docB.Size) { // Wrong sizes, can't have the same contents return false; } bool matches = true; DocumentInputStream inpA = null, inpB = null; try { inpA = new DocumentInputStream(docA); inpB = new DocumentInputStream(docB); int readA, readB; do { readA = inpA.Read(); readB = inpB.Read(); if (readA != readB) { matches = false; break; } } while (readA != -1 && readB != -1); } finally { if (inpA != null) inpA.Close(); if (inpB != null) inpB.Close(); } return matches; }
private void CheckAllDirectoryContents(DirectoryEntry dir) { IEnumerator<Entry> it = dir.Entries; //foreach (Entry entry in dir) while (it.MoveNext()) { Entry entry = it.Current; if (entry is DirectoryEntry) { CheckAllDirectoryContents((DirectoryEntry)entry); } else { DocumentNode doc = (DocumentNode)entry; DocumentInputStream dis = new DocumentInputStream(doc); try { int numBytes = dis.Available(); byte[] data = new byte[numBytes]; dis.Read(data); } finally { dis.Close(); } } } }
public void TestAvailable() { DocumentInputStream ostream = new DocumentInputStream(_workbook_o); DocumentInputStream nstream = new NDocumentInputStream(_workbook_n); Assert.AreEqual(_workbook_size, ostream.Available()); Assert.AreEqual(_workbook_size, nstream.Available()); ostream.Close(); nstream.Close(); try { ostream.Available(); Assert.Fail("Should have caught IOException"); } catch (InvalidOperationException) { // as expected } try { nstream.Available(); Assert.Fail("Should have caught IOException"); } catch (InvalidOperationException) { // as expected } }