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.

ClipboardData.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.hpsf;
  16. import org.apache.poi.util.IOUtils;
  17. import org.apache.poi.util.Internal;
  18. import org.apache.poi.util.LittleEndian;
  19. import org.apache.poi.util.LittleEndianByteArrayInputStream;
  20. import org.apache.poi.util.LittleEndianConsts;
  21. import org.apache.poi.util.POILogFactory;
  22. import org.apache.poi.util.POILogger;
  23. @Internal
  24. public class ClipboardData {
  25. //arbitrarily selected; may need to increase
  26. private static final int MAX_RECORD_LENGTH = 100_000_000;
  27. private static final POILogger LOG = POILogFactory.getLogger( ClipboardData.class );
  28. private int _format;
  29. private byte[] _value;
  30. public void read( LittleEndianByteArrayInputStream lei ) {
  31. int offset = lei.getReadIndex();
  32. long size = lei.readInt();
  33. if ( size < 4 ) {
  34. String msg =
  35. "ClipboardData at offset "+offset+" size less than 4 bytes "+
  36. "(doesn't even have format field!). Setting to format == 0 and hope for the best";
  37. LOG.log( POILogger.WARN, msg);
  38. _format = 0;
  39. _value = new byte[0];
  40. return;
  41. }
  42. _format = lei.readInt();
  43. _value = IOUtils.safelyAllocate(size - LittleEndianConsts.INT_SIZE, MAX_RECORD_LENGTH);
  44. lei.readFully(_value);
  45. }
  46. public byte[] getValue() {
  47. return _value;
  48. }
  49. public byte[] toByteArray() {
  50. byte[] result = new byte[LittleEndianConsts.INT_SIZE*2+_value.length];
  51. LittleEndian.putInt(result, 0, LittleEndianConsts.INT_SIZE + _value.length);
  52. LittleEndian.putInt(result, 4, _format);
  53. System.arraycopy(_value, 0, result, 8, _value.length);
  54. return result;
  55. }
  56. public void setValue( byte[] value ) {
  57. _value = value.clone();
  58. }
  59. }