1
package org.bouncycastle.openpgp;
4
import java.io.IOException;
5
import java.io.OutputStream;
8
import org.bouncycastle.bcpg.BCPGOutputStream;
9
import org.bouncycastle.bcpg.PacketTags;
10
import org.bouncycastle.util.Strings;
13
* Class for producing literal data packets.
15
public class PGPLiteralDataGenerator implements StreamGenerator
17
public static final char BINARY = PGPLiteralData.BINARY;
18
public static final char TEXT = PGPLiteralData.TEXT;
19
public static final char UTF8 = PGPLiteralData.UTF8;
22
* The special name indicating a "for your eyes only" packet.
24
public static final String CONSOLE = PGPLiteralData.CONSOLE;
27
* The special time for a modification time of "now" or
30
public static final Date NOW = PGPLiteralData.NOW;
32
private BCPGOutputStream pkOut;
33
private boolean oldFormat = false;
35
public PGPLiteralDataGenerator()
40
* Generates literal data objects in the old format, this is
41
* important if you need compatability with PGP 2.6.x.
45
public PGPLiteralDataGenerator(
48
this.oldFormat = oldFormat;
51
private void writeHeader(
55
long modificationTime)
60
byte[] encName = Strings.toUTF8ByteArray(name);
62
out.write((byte)encName.length);
64
for (int i = 0; i != encName.length; i++)
66
out.write(encName[i]);
69
long modDate = modificationTime / 1000;
71
out.write((byte)(modDate >> 24));
72
out.write((byte)(modDate >> 16));
73
out.write((byte)(modDate >> 8));
74
out.write((byte)(modDate));
78
* Open a literal data packet, returning a stream to store the data inside
81
* The stream created can be closed off by either calling close()
82
* on the stream or close() on the generator. Closing the returned
83
* stream does not close off the OutputStream parameter out.
85
* @param out the stream we want the packet in
86
* @param format the format we are using
87
* @param name the name of the "file"
88
* @param length the length of the data we will write
89
* @param modificationTime the time of last modification we want stored.
91
public OutputStream open(
96
Date modificationTime)
101
throw new IllegalStateException("generator already in open state");
104
pkOut = new BCPGOutputStream(out, PacketTags.LITERAL_DATA, length + 2 + name.length() + 4, oldFormat);
106
writeHeader(pkOut, format, name, modificationTime.getTime());
108
return new WrappedGeneratorStream(pkOut, this);
112
* Open a literal data packet, returning a stream to store the data inside
113
* the packet as an indefinite length stream. The stream is written out as a
114
* series of partial packets with a chunk size determined by the size of the
117
* The stream created can be closed off by either calling close()
118
* on the stream or close() on the generator. Closing the returned
119
* stream does not close off the OutputStream parameter out.
121
* <b>Note</b>: if the buffer is not a power of 2 in length only the largest power of 2
122
* bytes worth of the buffer will be used.
124
* @param out the stream we want the packet in
125
* @param format the format we are using
126
* @param name the name of the "file"
127
* @param modificationTime the time of last modification we want stored.
128
* @param buffer the buffer to use for collecting data to put into chunks.
130
public OutputStream open(
134
Date modificationTime,
140
throw new IllegalStateException("generator already in open state");
143
pkOut = new BCPGOutputStream(out, PacketTags.LITERAL_DATA, buffer);
145
writeHeader(pkOut, format, name, modificationTime.getTime());
147
return new WrappedGeneratorStream(pkOut, this);
151
* Open a literal data packet for the passed in File object, returning
152
* an output stream for saving the file contents.
154
* The stream created can be closed off by either calling close()
155
* on the stream or close() on the generator. Closing the returned
156
* stream does not close off the OutputStream parameter out.
161
* @return OutputStream
162
* @throws IOException
164
public OutputStream open(
172
throw new IllegalStateException("generator already in open state");
175
pkOut = new BCPGOutputStream(out, PacketTags.LITERAL_DATA, file.length() + 2 + file.getName().length() + 4, oldFormat);
177
writeHeader(pkOut, format, file.getName(), file.lastModified());
179
return new WrappedGeneratorStream(pkOut, this);
183
* Close the literal data packet - this is equivalent to calling close on the stream
184
* returned by the open() method.
186
* @throws IOException