I don't think it's necessary but it doesn't hurt either.
I'll add the following method:
/**Sets the data associated with the
stream
* @param data raw data, decrypted and uncompressed.
*/
public void setData(byte[] data) {
remove(PdfName.FILTER);
this.offset = -1;
if (Document.compress) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DeflaterOutputStream zip = new DeflaterOutputStream(stream);
zip.write(data);
zip.close();
bytes = stream.toByteArray();
}
catch (IOException ioe) {
throw new ExceptionConverter(ioe);
}
put(PdfName.FILTER, PdfName.FLATEDECODE);
}
else
bytes = data;
setLength(bytes.length);
}
* @param data raw data, decrypted and uncompressed.
*/
public void setData(byte[] data) {
remove(PdfName.FILTER);
this.offset = -1;
if (Document.compress) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DeflaterOutputStream zip = new DeflaterOutputStream(stream);
zip.write(data);
zip.close();
bytes = stream.toByteArray();
}
catch (IOException ioe) {
throw new ExceptionConverter(ioe);
}
put(PdfName.FILTER, PdfName.FLATEDECODE);
}
else
bytes = data;
setLength(bytes.length);
}
Best Regards,
Paulo Soares
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Perez Carmona, David
Sent: Thursday, August 26, 2004 11:51 AM
To: Itext-Questions (E-mail)
Subject: RE: [iText-questions] PRStream.getData() contributionAnother simple solution I propose, is to add this method to the PRStream class:/**Sets the data associated with the stream
* @param data Raw data, decrypted and uncompressed.*/
public void setData(byte[] data) throws PdfException, IOException {
remove(PdfName.FILTER);
bytes = data;
offset = -1;
setLength(data.length);
}in this way the object number doesn't change (because the reference stream is inside an object), and don't have to update references to it.I need this, because I'm doing an app that pre-process _javascript_ code inside the PDF file, and need to modify PRStream objects.If you find this small function useful, it can be added to the class.-----Mensaje original-----
De: Paulo Soares [mailto:[EMAIL PROTECTED]
Enviado el: jueves, 26 de agosto de 2004 12:30
Para: Perez Carmona, David
Asunto: RE: [iText-questions] PRStream.getData() contributionNo, create a new PdfStream to replace the original.Best Regards,Paulo Soares
From: Perez Carmona, David [mailto:[EMAIL PROTECTED]
Sent: Thursday, August 26, 2004 6:52 AM
To: Paulo Soares
Subject: RE: [iText-questions] PRStream.getData() contributionHi Paulo,Is there any opposite function like PdfReader.setStreamBytes(byte[] b), to set a stream data?Best regards,David-----Mensaje original-----
De: Paulo Soares [mailto:[EMAIL PROTECTED]
Enviado el: mi�rcoles, 25 de agosto de 2004 15:08
Para: Perez Carmona, David; Itext-Questions (E-mail)
Asunto: RE: [iText-questions] PRStream.getData() contributionYou already have that in PdfReader.getStreamBytes().Best Regards,Paulo Soares
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Perez Carmona, David
Sent: Wednesday, August 25, 2004 12:24 PM
To: Itext-Questions (E-mail)
Subject: [iText-questions] PRStream.getData() contributionHi all,I would like to contribute with a new method I�ve added to the class PRStream, it allows to get the bytes of a stream (decrypted and deflated if necessary).Here is its code:/**Get the data associated with the stream, deflated and decrypted if necessary.*/
public byte[] getData() throws IOException, PdfException {
if (length == 0) {
return new byte[0];
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
if (length > 0) {
if (offset < 0) {
bos.write(bytes);
} else {
byte buf[] = new byte[Math.min(length, 4092)];
RandomAccessFileOrArray file = reader.getSafeFile();
file.seek(offset);
int size = length;
PdfEncryption decrypt = reader.getDecrypt();
if (decrypt != null) {
decrypt.setHashKey(objNum, objGen);
decrypt.prepareKey();
}
while (size > 0) {
int r = file.read(buf, 0, Math.min(size, buf.length));
size -= r;
if (decrypt != null)
decrypt.encryptRC4(buf, 0, r); //added by ujihara for decryption
bos.write(buf, 0, r);
}
}
}
boolean compressed = false;
PdfObject filter = get(PdfName.FILTER);
if (filter != null) {
if (filter.isName() && ((PdfName) filter).compareTo(PdfName.FLATEDECODE) == 0) {
compressed = true;
}
else if (filter.isArray() && ((PdfArray) filter).contains(PdfName.FLATEDECODE)) {
compressed = true;
}
else {
throw new PdfException("Stream could not be compressed: filter is not a name or array.");
}
}
if (compressed) {
InputStream is = new InflaterInputStream(new ByteArrayInputStream(bos.toByteArray()));
bos.reset();
while (true) {
int c = is.read();
if (c < 0) {
break;
}
bos.write(c);
}
}
return bos.toByteArray();
}
************************************************************* Este correo ha sido procesado por el Antivirus del Grupo FCC *************************************************************
************************************************************* Este correo ha sido procesado por el Antivirus del Grupo FCC. *************************************************************
************************************************************* Este correo ha sido procesado por el Antivirus del Grupo FCC *************************************************************
************************************************************* Este correo ha sido procesado por el antivirus del Grupo FCC. *************************************************************
************************************************************* Este correo ha sido procesado por el Antivirus del Grupo FCC *************************************************************
