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.hslf.record;
019:
020: import org.apache.poi.util.LittleEndian;
021: import org.apache.poi.util.StringUtil;
022: import java.io.IOException;
023: import java.io.OutputStream;
024:
025: /**
026: * A TextCharsAtom (type 4000). Holds text in byte swapped unicode form.
027: * The trailing return character is always stripped from this
028: *
029: * @author Nick Burch
030: */
031:
032: public class TextCharsAtom extends RecordAtom {
033: private byte[] _header;
034: private static long _type = 4000l;
035:
036: /** The bytes that make up the text */
037: private byte[] _text;
038:
039: /** Grabs the text. */
040: public String getText() {
041: return StringUtil.getFromUnicodeLE(_text);
042: }
043:
044: /** Updates the text in the Atom. */
045: public void setText(String text) {
046: // Convert to little endian unicode
047: _text = new byte[text.length() * 2];
048: StringUtil.putUnicodeLE(text, _text, 0);
049:
050: // Update the size (header bytes 5-8)
051: LittleEndian.putInt(_header, 4, _text.length);
052: }
053:
054: /* *************** record code follows ********************** */
055:
056: /**
057: * For the TextChars Atom
058: */
059: protected TextCharsAtom(byte[] source, int start, int len) {
060: // Sanity Checking
061: if (len < 8) {
062: len = 8;
063: }
064:
065: // Get the header
066: _header = new byte[8];
067: System.arraycopy(source, start, _header, 0, 8);
068:
069: // Grab the text
070: _text = new byte[len - 8];
071: System.arraycopy(source, start + 8, _text, 0, len - 8);
072: }
073:
074: /**
075: * Create an empty TextCharsAtom
076: */
077: public TextCharsAtom() {
078: // 0 length header
079: _header = new byte[] { 0, 0, 0xA0 - 256, 0x0f, 0, 0, 0, 0 };
080: // Empty text
081: _text = new byte[0];
082: }
083:
084: /**
085: * We are of type 4000
086: */
087: public long getRecordType() {
088: return _type;
089: }
090:
091: /**
092: * Write the contents of the record back, so it can be written
093: * to disk
094: */
095: public void writeOut(OutputStream out) throws IOException {
096: // Header - size or type unchanged
097: out.write(_header);
098:
099: // Write out our text
100: out.write(_text);
101: }
102: }
|