You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SavedByTable.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwpf.model;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import org.apache.poi.util.Internal;
  22. /**
  23. * String table containing the history of the last few revisions ("saves") of
  24. * the document. Read-only for the time being.
  25. *
  26. * @author Daniel Noll
  27. */
  28. @Internal
  29. public final class SavedByTable
  30. {
  31. /**
  32. * Array of entries.
  33. */
  34. private SavedByEntry[] entries;
  35. /**
  36. * Constructor to read the table from the table stream.
  37. *
  38. * @param tableStream
  39. * the table stream.
  40. * @param offset
  41. * the offset into the byte array.
  42. * @param size
  43. * the size of the table in the byte array.
  44. */
  45. public SavedByTable( byte[] tableStream, int offset, int size )
  46. {
  47. String[] strings = SttbUtils.readSttbSavedBy( tableStream, offset );
  48. int numEntries = strings.length / 2;
  49. entries = new SavedByEntry[numEntries];
  50. for ( int i = 0; i < numEntries; i++ )
  51. {
  52. entries[i] = new SavedByEntry( strings[i * 2], strings[i * 2 + 1] );
  53. }
  54. }
  55. /**
  56. * Gets the entries. The returned list cannot be modified.
  57. *
  58. * @return the list of entries.
  59. */
  60. public List<SavedByEntry> getEntries()
  61. {
  62. return Collections.unmodifiableList( Arrays.asList( entries ) );
  63. }
  64. /**
  65. * Writes this table to the table stream.
  66. *
  67. * @param tableStream
  68. * the table stream to write to.
  69. * @throws IOException
  70. * if an error occurs while writing.
  71. */
  72. public void writeTo( ByteArrayOutputStream tableStream ) throws IOException
  73. {
  74. String[] toSave = new String[entries.length * 2];
  75. int counter = 0;
  76. for ( SavedByEntry entry : entries )
  77. {
  78. toSave[counter++] = entry.getUserName();
  79. toSave[counter++] = entry.getSaveLocation();
  80. }
  81. SttbUtils.writeSttbSavedBy( toSave, tableStream );
  82. }
  83. }