Beispiel #1
0
 /**
  * Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted
  * back to their original representation.
  *
  * <p>
  * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
  * RFC 1521.
  * </p>
  *
  * @param bytes
  *                  array of quoted-printable characters
  * @return array of original bytes
  * @throws DecoderException
  *                  Thrown if quoted-printable decoding is unsuccessful
  */
 public static byte[] decodeQuotedPrintable(byte[] bytes)
 {// throws DecoderException {
     if (bytes == null)
     {
         return(null);
     }
     java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();
     for (int i = 0; i < bytes.Length; i++)
     {
         int b = bytes[i];
         if (b == ESCAPE_CHAR)
         {
             try
             {
                 int u = Utils.digit16(bytes[++i]);
                 int l = Utils.digit16(bytes[++i]);
                 buffer.write((char)((u << 4) + l));
             }
             catch (java.lang.ArrayIndexOutOfBoundsException e)
             {
                 throw new DecoderException("Invalid quoted-printable encoding", e);
             }
         }
         else
         {
             buffer.write(b);
         }
     }
     return(buffer.toByteArray());
 }
Beispiel #2
0
 /**
  * Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.
  *
  * <p>
  * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
  * RFC 1521 and is suitable for encoding binary data and unformatted text.
  * </p>
  *
  * @param printable
  *                  bitset of characters deemed quoted-printable
  * @param bytes
  *                  array of bytes to be encoded
  * @return array of bytes containing quoted-printable data
  */
 public static byte[] encodeQuotedPrintable(java.util.BitSet printable, byte[] bytes)
 {
     if (bytes == null)
     {
         return(null);
     }
     if (printable == null)
     {
         printable = PRINTABLE_CHARS;
     }
     java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();
     for (int i = 0; i < bytes.Length; i++)
     {
         int b = bytes[i];
         if (b < 0)
         {
             b = 256 + b;
         }
         if (printable.get(b))
         {
             buffer.write(b);
         }
         else
         {
             encodeQuotedPrintable(b, buffer);
         }
     }
     return(buffer.toByteArray());
 }
Beispiel #3
0
        /*
         * Decodes the string argument which is assumed to be encoded in the {@code
         * x-www-form-urlencoded} MIME content type using the UTF-8 encoding scheme.
         * <p/>
         *'%' and two following hex digit characters are converted to the
         * equivalent byte value. All other characters are passed through
         * unmodified.
         * <p/>
         * e.g. "A%20B%20C %24%25" -> "A B C $%"
         * <p/>
         * Called from URI.getXYZ() methods
         *
         * @param s
         *            java.lang.String The encoded string.
         * @return java.lang.String The decoded version.
         */
        static String decode(String s)  //throws UnsupportedEncodingException {
        {
            StringBuilder result = new StringBuilder();

            java.io.ByteArrayOutputStream outJ = new java.io.ByteArrayOutputStream();
            for (int i = 0; i < s.length();)
            {
                char c = s.charAt(i);
                if (c == '%')
                {
                    outJ.reset();
                    do
                    {
                        if (i + 2 >= s.length())
                        {
                            throw new java.lang.IllegalArgumentException("Incomplete % sequence at: " + i); //$NON-NLS-1$
                        }
                        int d1 = java.lang.Character.digit(s.charAt(i + 1), 16);
                        int d2 = java.lang.Character.digit(s.charAt(i + 2), 16);
                        if (d1 == -1 || d2 == -1)
                        {
                            throw new java.lang.IllegalArgumentException("Invalid % sequence (" + s.substring(i, i + 3) + ") at: " + java.lang.StringJ.valueOf(i));
                        }
                        outJ.write((byte)((d1 << 4) + d2));
                        i += 3;
                    } while (i < s.length() && s.charAt(i) == '%');
                    result.append(outJ.toString(encoding));
                    continue;
                }
                result.append(c);
                i++;
            }
            return(result.toString());
        }
Beispiel #4
0
        /*
         * Reads all the bytes from the given input stream.
         *
         * Calls read multiple times on the given input stream until it receives an
         * end of file marker. Returns the combined results as a byte array. Note
         * that this method may block if the underlying stream read blocks.
         *
         * @param is
         *            the input stream to be read.
         * @return the content of the stream as a byte array.
         * @throws IOException
         *             if a read error occurs.
         */
        public static byte[] readFullyAndClose(java.io.InputStream isJ) // throws IOException {
        {
            try {
                // Initial read
                byte[] buffer   = new byte[1024];
                int    count    = isJ.read(buffer);
                int    nextByte = isJ.read();

                // Did we get it all in one read?
                if (nextByte == -1)
                {
                    byte[] dest = new byte[count];
                    java.lang.SystemJ.arraycopy(buffer, 0, dest, 0, count);
                    return(dest);
                }

                // Requires additional reads
                java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(count * 2);
                baos.write(buffer, 0, count);
                baos.write(nextByte);
                while (true)
                {
                    count = isJ.read(buffer);
                    if (count == -1)
                    {
                        return(baos.toByteArray());
                    }
                    baos.write(buffer, 0, count);
                }
            } finally {
                isJ.close();
            }
        }
Beispiel #5
0
        /**
         * Encodes byte into its quoted-printable representation.
         *
         * @param b
         *                  byte to encode
         * @param buffer
         *                  the buffer to write to
         */
        private static void encodeQuotedPrintable(int b, java.io.ByteArrayOutputStream buffer)
        {
            buffer.write(ESCAPE_CHAR);
            char hex1 = java.lang.Character.toUpperCase(java.lang.Character.forDigit((b >> 4) & 0xF, 16));
            char hex2 = java.lang.Character.toUpperCase(java.lang.Character.forDigit(b & 0xF, 16));

            buffer.write(hex1);
            buffer.write(hex2);
        }
        static internal byte [] toByteArray(java.io.InputStream inJ)
        {
            java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
            byte[] buffer = new byte[32 * 1024];

            int bytesRead;

            while ((bytesRead = inJ.read(buffer)) > 0)
            {
                baos.write(buffer, 0, bytesRead);
            }
            byte[] bytes = baos.toByteArray();
            return(bytes);
        }
Beispiel #7
0
            /**
             * Creates an object using serialization.
             *
             * @return the new object
             */
            public Object create()
            {
                java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(512);
                java.io.ByteArrayInputStream  bais = null;
                try
                {
                    java.io.ObjectOutputStream outJ = new java.io.ObjectOutputStream(baos);
                    outJ.writeObject(iPrototype);

                    bais = new java.io.ByteArrayInputStream(baos.toByteArray());
                    java.io.ObjectInputStream inJ = new java.io.ObjectInputStream(bais);
                    return(inJ.readObject());
                }
                catch (java.lang.ClassNotFoundException ex)
                {
                    throw new FunctorException(ex);
                }
                catch (java.io.IOException ex)
                {
                    throw new FunctorException(ex);
                }
                finally
                {
                    try
                    {
                        if (bais != null)
                        {
                            bais.close();
                        }
                    }
                    catch (java.io.IOException ex)
                    {
                        // ignore
                    }
                    try
                    {
                        if (baos != null)
                        {
                            baos.close();
                        }
                    }
                    catch (java.io.IOException ex)
                    {
                        // ignore
                    }
                }
            }
Beispiel #8
0
        /*
         * Indicates that all entries have been written to the stream. Any terminal
         * information is written to the underlying stream.
         *
         * @throws IOException
         *             if an error occurs while terminating the stream.
         */

        public override void finish()  //throws IOException {
        {
            if (outJ == null)
            {
                throw new java.io.IOException("Stream is closed"); //$NON-NLS-1$
            }
            if (cDir == null)
            {
                return;
            }
            if (entries.size() == 0)
            {
                throw new ZipException("No entries"); //$NON-NLS-1$;
            }
            if (currentEntry != null)
            {
                closeEntry();
            }
            int cdirSize = cDir.size();

            // Write Central Dir End
            writeLong(cDir, ZipFile.ENDSIG);
            writeShort(cDir, 0);              // Disk Number
            writeShort(cDir, 0);              // Start Disk
            writeShort(cDir, entries.size()); // Number of entries
            writeShort(cDir, entries.size()); // Number of entries
            writeLong(cDir, cdirSize);        // Size of central dir
            writeLong(cDir, offset);          // Offset of central dir
            if (comment != null)
            {
                writeShort(cDir, comment.length());
                cDir.write(comment.getBytes());
            }
            else
            {
                writeShort(cDir, 0);
            }
            // Write the central dir
            outJ.write(cDir.toByteArray());
            cDir = null;
        }
Beispiel #9
0
        /**
         * Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped.
         *
         * @param urlsafe
         *            bitset of characters deemed URL safe
         * @param bytes
         *            array of bytes to convert to URL safe characters
         * @return array of bytes containing URL safe characters
         */
        public static byte[] encodeUrl(java.util.BitSet urlsafe, byte[] bytes)
        {
            if (bytes == null)
            {
                return(null);
            }
            if (urlsafe == null)
            {
                urlsafe = WWW_FORM_URL;
            }

            java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();
            for (int i = 0; i < bytes.Length; i++)
            {
                int b = bytes[i];
                if (b < 0)
                {
                    b = 256 + b;
                }
                if (urlsafe.get(b))
                {
                    if (b == ' ')
                    {
                        b = '+';
                    }
                    buffer.write(b);
                }
                else
                {
                    buffer.write(ESCAPE_CHAR);
                    char hex1 = java.lang.Character.toUpperCase(java.lang.Character.forDigit((b >> 4) & 0xF, RADIX));
                    char hex2 = java.lang.Character.toUpperCase(java.lang.Character.forDigit(b & 0xF, RADIX));
                    buffer.write(hex1);
                    buffer.write(hex2);
                }
            }
            return(buffer.toByteArray());
        }
        public static void main(String[] args)
        {
            try
            {
                java.io.ByteArrayOutputStream outJ = new java.io.ByteArrayOutputStream();
                ZOutputStream zOut = new ZOutputStream(outJ, JZlib.Z_BEST_COMPRESSION);
                String hello = "Hello World!";
                //java.io.ObjectOutputStream objOut = new java.io.ObjectOutputStream(zOut);
                //objOut.writeObject(hello);
                outJ.write(hello.getBytes());
                zOut.close();

                java.io.ByteArrayInputStream inJ = new java.io.ByteArrayInputStream(outJ.toByteArray());
                ZInputStream zIn = new ZInputStream(inJ);
                //java.io.ObjectInputStream objIn=new java.io.ObjectInputStream(zIn);
                byte[] buffer = new byte[hello.length()];
                inJ.read(buffer);
                java.lang.SystemJ.outJ.println(new java.lang.StringJ(buffer).ToString() /*objIn.readObject()*/);
            }
            catch (java.lang.Exception e)
            {
                e.printStackTrace();
            }
        }
        public static void main(String[] args)
        {
            try
            {
                java.io.ByteArrayOutputStream outJ = new java.io.ByteArrayOutputStream();
                ZOutputStream zOut  = new ZOutputStream(outJ, JZlib.Z_BEST_COMPRESSION);
                String        hello = "Hello World!";
                //java.io.ObjectOutputStream objOut = new java.io.ObjectOutputStream(zOut);
                //objOut.writeObject(hello);
                outJ.write(hello.getBytes());
                zOut.close();

                java.io.ByteArrayInputStream inJ = new java.io.ByteArrayInputStream(outJ.toByteArray());
                ZInputStream zIn = new ZInputStream(inJ);
                //java.io.ObjectInputStream objIn=new java.io.ObjectInputStream(zIn);
                byte[] buffer = new byte[hello.length()];
                inJ.read(buffer);
                java.lang.SystemJ.outJ.println(new java.lang.StringJ(buffer).ToString() /*objIn.readObject()*/);
            }
            catch (java.lang.Exception e)
            {
                e.printStackTrace();
            }
        }
        /*
         * Helper to read the entire contents of the manifest from the
         * given input stream.  Usually we can do this in a single read
         * but we need to account for 'infinite' streams, by ensuring we
         * have a line feed within a reasonable number of characters.
         */
        private byte[] readFully(java.io.InputStream isJ) // throws IOException {
        // Initial read
        {
            byte[] buffer   = new byte[4096];
            int    count    = isJ.read(buffer);
            int    nextByte = isJ.read();

            // Did we get it all in one read?
            if (nextByte == -1)
            {
                byte[] dest = new byte[count];
                java.lang.SystemJ.arraycopy(buffer, 0, dest, 0, count);
                return(dest);
            }

            // Does it look like a manifest?
            if (!containsLine(buffer, count))
            {
                // archive.2E=Manifest is too long
                throw new java.io.IOException("Manifest is too long"); //$NON-NLS-1$
            }

            // Requires additional reads
            java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(count * 2);
            baos.write(buffer, 0, count);
            baos.write(nextByte);
            while (true)
            {
                count = isJ.read(buffer);
                if (count == -1)
                {
                    return(baos.toByteArray());
                }
                baos.write(buffer, 0, count);
            }
        }
 /**
  * Indicates that all entries have been written to the stream. Any terminal
  * information is written to the underlying stream.
  *
  * @throws IOException
  *             if an error occurs while terminating the stream.
  */
 public override void finish()
 {
     //throws IOException {
     if (outJ == null) {
         throw new java.io.IOException("Stream is closed"); //$NON-NLS-1$
     }
     if (cDir == null) {
         return;
     }
     if (entries.size() == 0) {
         throw new ZipException("No entries"); //$NON-NLS-1$;
     }
     if (currentEntry != null) {
         closeEntry();
     }
     int cdirSize = cDir.size();
     // Write Central Dir End
     writeLong(cDir, ZipFile.ENDSIG);
     writeShort(cDir, 0); // Disk Number
     writeShort(cDir, 0); // Start Disk
     writeShort(cDir, entries.size()); // Number of entries
     writeShort(cDir, entries.size()); // Number of entries
     writeLong(cDir, cdirSize); // Size of central dir
     writeLong(cDir, offset); // Offset of central dir
     if (comment != null) {
         writeShort(cDir, comment.length());
         cDir.write(comment.getBytes());
     } else {
         writeShort(cDir, 0);
     }
     // Write the central dir
     outJ.write(cDir.toByteArray());
     cDir = null;
 }
        /**
         * Reads all the bytes from the given input stream.
         *
         * Calls read multiple times on the given input stream until it receives an
         * end of file marker. Returns the combined results as a byte array. Note
         * that this method may block if the underlying stream read blocks.
         *
         * @param is
         *            the input stream to be read.
         * @return the content of the stream as a byte array.
         * @throws IOException
         *             if a read error occurs.
         */
        public static byte[] readFullyAndClose(java.io.InputStream isJ)
        {
            // throws IOException {

            try {
                // Initial read
                byte[] buffer = new byte[1024];
                int count = isJ.read(buffer);
                int nextByte = isJ.read();

                // Did we get it all in one read?
                if (nextByte == -1) {
                    byte[] dest = new byte[count];
                    java.lang.SystemJ.arraycopy(buffer, 0, dest, 0, count);
                    return dest;
                }

                // Requires additional reads
                java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(count * 2);
                baos.write(buffer, 0, count);
                baos.write(nextByte);
                while (true) {
                    count = isJ.read(buffer);
                    if (count == -1) {
                        return baos.toByteArray();
                    }
                    baos.write(buffer, 0, count);
                }
            } finally {
                isJ.close();
            }
        }
        /*
         * Helper to read the entire contents of the manifest from the
         * given input stream.  Usually we can do this in a single read
         * but we need to account for 'infinite' streams, by ensuring we
         * have a line feed within a reasonable number of characters.
         */
        private byte[] readFully(java.io.InputStream isJ)
        {
            // throws IOException {
            // Initial read
            byte[] buffer = new byte[4096];
            int count = isJ.read(buffer);
            int nextByte = isJ.read();

            // Did we get it all in one read?
            if (nextByte == -1) {
                byte[] dest = new byte[count];
                java.lang.SystemJ.arraycopy(buffer, 0, dest, 0, count);
                return dest;
            }

            // Does it look like a manifest?
            if (!containsLine(buffer, count)) {
                // archive.2E=Manifest is too long
                throw new java.io.IOException("Manifest is too long"); //$NON-NLS-1$
            }

            // Requires additional reads
            java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(count * 2);
            baos.write(buffer, 0, count);
            baos.write(nextByte);
            while (true) {
                count = isJ.read(buffer);
                if (count == -1) {
                    return baos.toByteArray();
                }
                baos.write(buffer, 0, count);
            }
        }
        /**
         * Caches a stored entry that uses the data descriptor.
         *
         * <ul>
         *   <li>Reads a stored entry until the signature of a local file
         *     header, central directory header or data descriptor has been
         *     found.</li>
         *   <li>Stores all entry data in lastStoredEntry.</p>
         *   <li>Rewinds the stream to position at the data
         *     descriptor.</li>
         *   <li>reads the data descriptor</li>
         * </ul>
         *
         * <p>After calling this method the entry should know its size,
         * the entry's data is cached and the stream is positioned at the
         * next local file or central directory header.</p>
         */
        private void readStoredEntry() //throws IOException
        {
            java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
            byte[] LFH  = ZipLong.LFH_SIG.getBytes();
            byte[] CFH  = ZipLong.CFH_SIG.getBytes();
            byte[] DD   = ZipLong.DD_SIG.getBytes();
            int    off  = 0;
            bool   done = false;

            while (!done)
            {
                int r = inJ.read(buf, off, ZipArchiveOutputStream.BUFFER_SIZE - off);
                if (r <= 0)
                {
                    // read the whole archive without ever finding a
                    // central directory
                    throw new java.io.IOException("Truncated ZIP file");
                }
                if (r + off < 4)
                {
                    // buf is too small to check for a signature, loop
                    off += r;
                    continue;
                }

                int readTooMuch = 0;
                for (int i = 0; !done && i < r - 4; i++)
                {
                    if (buf[i] == LFH[0] && buf[i + 1] == LFH[1])
                    {
                        if ((buf[i + 2] == LFH[2] && buf[i + 3] == LFH[3]) ||
                            (buf[i] == CFH[2] && buf[i + 3] == CFH[3]))
                        {
                            // found a LFH or CFH:
                            readTooMuch = off + r - i - 12 /* dd without signature */;
                            done        = true;
                        }
                        else if (buf[i + 2] == DD[2] && buf[i + 3] == DD[3])
                        {
                            // found DD:
                            readTooMuch = off + r - i;
                            done        = true;
                        }
                        if (done)
                        {
                            // * push back bytes read in excess as well as the data
                            //   descriptor
                            // * copy the remaining bytes to cache
                            // * read data descriptor
                            ((java.io.PushbackInputStream)inJ).unread(buf, off + r - readTooMuch, readTooMuch);
                            bos.write(buf, 0, i);
                            readDataDescriptor();
                        }
                    }
                }
                if (!done)
                {
                    // worst case we've read a data descriptor without a
                    // signature (12 bytes) plus the first three bytes of
                    // a LFH or CFH signature
                    // save the last 15 bytes in the buffer, cache
                    // anything in front of that, read on
                    if (off + r > 15)
                    {
                        bos.write(buf, 0, off + r - 15);
                        java.lang.SystemJ.arraycopy(buf, off + r - 15, buf, 0, 15);
                        off = 15;
                    }
                    else
                    {
                        off += r;
                    }
                }
            }

            byte[] b = bos.toByteArray();
            lastStoredEntry = new java.io.ByteArrayInputStream(b);
        }
        /**
         * Decodes the string argument which is assumed to be encoded in the {@code
         * x-www-form-urlencoded} MIME content type using the UTF-8 encoding scheme.
         * <p>
         *'%' and two following hex digit characters are converted to the
         * equivalent byte value. All other characters are passed through
         * unmodified.
         * <p>
         * e.g. "A%20B%20C %24%25" -> "A B C $%"
         * <p>
         * Called from URI.getXYZ() methods
         *
         * @param s
         *            java.lang.String The encoded string.
         * @return java.lang.String The decoded version.
         */
        static String decode(String s)
        {
            //throws UnsupportedEncodingException {

            StringBuilder result = new StringBuilder();
            java.io.ByteArrayOutputStream outJ = new java.io.ByteArrayOutputStream();
            for (int i = 0; i < s.length();) {
                char c = s.charAt(i);
                if (c == '%') {
                    outJ.reset();
                    do {
                        if (i + 2 >= s.length()) {
                            throw new java.lang.IllegalArgumentException("Incomplete % sequence at: "+ i); //$NON-NLS-1$
                        }
                        int d1 = java.lang.Character.digit(s.charAt(i + 1), 16);
                        int d2 = java.lang.Character.digit(s.charAt(i + 2), 16);
                        if (d1 == -1 || d2 == -1) {
                            throw new java.lang.IllegalArgumentException("Invalid % sequence ("+s.substring(i, i + 3)+") at: "+java.lang.StringJ.valueOf(i));
                        }
                        outJ.write((byte) ((d1 << 4) + d2));
                        i += 3;
                    } while (i < s.length() && s.charAt(i) == '%');
                    result.append(outJ.toString(encoding));
                    continue;
                }
                result.append(c);
                i++;
            }
            return result.toString();
        }
            /**
             * Creates an object using serialization.
             *
             * @return the new object
             */
            public Object create()
            {
                java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(512);
                java.io.ByteArrayInputStream bais = null;
                try
                {
                    java.io.ObjectOutputStream outJ = new java.io.ObjectOutputStream(baos);
                    outJ.writeObject(iPrototype);

                    bais = new java.io.ByteArrayInputStream(baos.toByteArray());
                    java.io.ObjectInputStream inJ = new java.io.ObjectInputStream(bais);
                    return inJ.readObject();

                }
                catch (java.lang.ClassNotFoundException ex)
                {
                    throw new FunctorException(ex);
                }
                catch (java.io.IOException ex)
                {
                    throw new FunctorException(ex);
                }
                finally
                {
                    try
                    {
                        if (bais != null)
                        {
                            bais.close();
                        }
                    }
                    catch (java.io.IOException ex)
                    {
                        // ignore
                    }
                    try
                    {
                        if (baos != null)
                        {
                            baos.close();
                        }
                    }
                    catch (java.io.IOException ex)
                    {
                        // ignore
                    }
                }
            }