Java/Development Class/Ascii Code

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add a ASCII85 encoding to the stream

     
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* $Id: ASCII85OutputStream.java 426584 2006-07-28 16:01:47Z jeremias $ */

import java.io.OutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
/**
 * This class applies a ASCII85 encoding to the stream.
 *
 * @version $Id: ASCII85OutputStream.java 426584 2006-07-28 16:01:47Z jeremias $
 */
public class ASCII85OutputStream extends FilterOutputStream
            implements ASCII85Constants {
    private static final boolean DEBUG = false;
    private int pos = 0;
    private long buffer = 0;
    private int posinline = 0;
    private int bw = 0;
    /** @see java.io.FilterOutputStream **/
    public ASCII85OutputStream(OutputStream out) {
        super(out);
    }
    /** @see java.io.FilterOutputStream **/
    public void write(int b) throws IOException {
        if (pos == 0) {
            buffer += (b << 24) & 0xff000000L;
        } else if (pos == 1) {
            buffer += (b << 16) & 0xff0000L;
        } else if (pos == 2) {
            buffer += (b << 8) & 0xff00L;
        } else {
            buffer += b & 0xffL;
        }
        pos++;
        if (pos > 3) {
            checkedWrite(convertWord(buffer));
            buffer = 0;
            pos = 0;
        }
    }
    /* UNUSED ATM
    private void checkedWrite(int b) throws IOException {
        if (posinline == 80) {
            out.write(EOL); bw++;
            posinline = 0;
        }
        checkedWrite(b);
        posinline++;
        bw++;
    }*/
    private void checkedWrite(byte[] buf) throws IOException {
        checkedWrite(buf, buf.length, false);
    }
    private void checkedWrite(byte[] buf, boolean nosplit) throws IOException {
        checkedWrite(buf, buf.length, nosplit);
    }
    private void checkedWrite(byte[] buf , int len) throws IOException {
        checkedWrite(buf, len, false);
    }
    private void checkedWrite(byte[] buf , int len, boolean nosplit) throws IOException {
        if (posinline + len > 80) {
            int firstpart = (nosplit ? 0 : len - (posinline + len - 80));
            if (firstpart > 0) {
                out.write(buf, 0, firstpart);
            }
            out.write(EOL); bw++;
            int rest = len - firstpart;
            if (rest > 0) {
                out.write(buf, firstpart, rest);
            }
            posinline = rest;
        } else {
            out.write(buf, 0, len);
            posinline += len;
        }
        bw += len;
    }
    /**
     * This converts a 32 bit value (4 bytes) into 5 bytes using base 85.
     * each byte in the result starts with zero at the "!" character so
     * the resulting base85 number fits into printable ascii chars
     *
     * @param word the 32 bit unsigned (hence the long datatype) word
     * @return 5 bytes (or a single byte of the "z" character for word
     * values of 0)
     */
    private byte[] convertWord(long word) {
        word = word & 0xffffffff;
        if (word == 0) {
            return ZERO_ARRAY;
        } else {
            if (word < 0) {
                word = -word;
            }
            byte c1 =
                (byte)((word
                        / POW85[0]) & 0xFF);
            byte c2 =
                (byte)(((word - (c1 * POW85[0]))
                        / POW85[1]) & 0xFF);
            byte c3 =
                (byte)(((word - (c1 * POW85[0])
                              - (c2 * POW85[1]))
                        / POW85[2]) & 0xFF);
            byte c4 =
                (byte)(((word - (c1 * POW85[0])
                              - (c2 * POW85[1])
                              - (c3 * POW85[2]))
                        / POW85[3]) & 0xFF);
            byte c5 =
                (byte)(((word - (c1 * POW85[0])
                              - (c2 * POW85[1])
                              - (c3 * POW85[2])
                              - (c4 * POW85[3])))
                        & 0xFF);
            byte[] ret = {
                (byte)(c1 + START), (byte)(c2 + START),
                (byte)(c3 + START), (byte)(c4 + START),
                (byte)(c5 + START)
            };
            if (DEBUG) {
                for (int i = 0; i < ret.length; i++) {
                    if (ret[i] < 33 || ret[i] > 117) {
                        System.out.println("Illegal char value "
                                        + new Integer(ret[i]));
                    }
                }
            }
            return ret;
        }
    }
    /** @see Finalizable **/
    public void finalizeStream() throws IOException {
        // now take care of the trailing few bytes.
        // with n leftover bytes, we append 0 bytes to make a full group of 4
        // then convert like normal (except not applying the special zero rule)
        // and write out the first n+1 bytes from the result
        if (pos > 0) {
            int rest = pos;
            /*
            byte[] lastdata = new byte[4];
            int i = 0;
            for (int j = 0; j < 4; j++) {
                if (j < rest) {
                    lastdata[j] = data[i++];
                } else {
                    lastdata[j] = 0;
                }
            }
            long val = ((lastdata[0] << 24) & 0xff000000L)
                       + ((lastdata[1] << 16) & 0xff0000L)
                       + ((lastdata[2] << 8) & 0xff00L)
                       + (lastdata[3] & 0xffL);
            */
            byte[] conv;
            // special rule for handling zeros at the end
            if (buffer != 0) {
                conv = convertWord(buffer);
            } else {
                conv = new byte[5];
                for (int j = 0; j < 5; j++) {
                    conv[j] = (byte)"!";
                }
            }
            // assert rest+1 <= 5
            checkedWrite(conv, rest + 1);
        }
        // finally write the two character end of data marker
        checkedWrite(EOD, true);
        flush();
  
    }
    /** @see java.io.FilterOutputStream **/
    public void close() throws IOException {
        finalizeStream();
        super.close();
    }
}


/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* $Id: ASCII85Constants.java 426584 2006-07-28 16:01:47Z jeremias $ */
 
/**
 * This interface defines constants used by the ASCII85 filters.
 *
 * @version $Id: ASCII85Constants.java 426584 2006-07-28 16:01:47Z jeremias $
 */
interface ASCII85Constants {
    /** Special character "z" stands for four NULL bytes (short-cut for !!!!!) */
    public static final int ZERO          = 0x7A; //"z"
    /** ZERO as a byte array */
    public static final byte[] ZERO_ARRAY = {(byte)ZERO};
    /** The start index for ASCII85 characters (!) */
    public static final int START         = 0x21; //"!"
    /** The end index for ASCII85 characters (u) */
    public static final int END           = 0x75; //"u"
    /** The EOL indicator (LF) */
    public static final int EOL           = 0x0A; //"\n"
    /** The EOD (end of data) indicator */
    public static final byte[] EOD        = {0x7E, 0x3E}; //"~>"
    /** Array of powers of 85 (4, 3, 2, 1, 0) */
    public static final long POW85[] = new long[] {85 * 85 * 85 * 85, 
                                                    85 * 85 * 85,
                                                    85 * 85,
                                                    85,
                                                    1};
    /*
    public static final long BASE85_4 = 85;
    public static final long BASE85_3 = BASE85_4 * BASE85_4;
    public static final long BASE85_2 = BASE85_3 * BASE85_4;
    public static final long BASE85_1 = BASE85_2 * BASE85_4;
    */
}





ASCII Utility

     
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License. You can obtain
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the GPL Version 2 section of the License file that
 * accompanied this code.  If applicable, add the following below the License
 * Header, with the fields enclosed by brackets [] replaced by your own
 * identifying information: "Portions Copyrighted [year]
 * [name of copyright owner]"
 *
 * Contributor(s):
 *
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don"t indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
/*
 * @(#)ASCIIUtility.java  1.11 07/05/04
 */
 class ASCIIUtility {
    // Private constructor so that this class is not instantiated
    private ASCIIUtility() { }
  
    /**
     * Convert the bytes within the specified range of the given byte 
     * array into a signed integer in the given radix . The range extends 
     * from <code>start</code> till, but not including <code>end</code>. <p>
     *
     * Based on java.lang.Integer.parseInt()
     */
    public static int parseInt(byte[] b, int start, int end, int radix)
    throws NumberFormatException {
  if (b == null)
      throw new NumberFormatException("null");
  
  int result = 0;
  boolean negative = false;
  int i = start;
  int limit;
  int multmin;
  int digit;
  if (end > start) {
      if (b[i] == "-") {
    negative = true;
    limit = Integer.MIN_VALUE;
    i++;
      } else {
    limit = -Integer.MAX_VALUE;
      }
      multmin = limit / radix;
      if (i < end) {
    digit = Character.digit((char)b[i++], radix);
    if (digit < 0) {
        throw new NumberFormatException(
      "illegal number: " + toString(b, start, end)
      );
    } else {
        result = -digit;
    }
      }
      while (i < end) {
    // Accumulating negatively avoids surprises near MAX_VALUE
    digit = Character.digit((char)b[i++], radix);
    if (digit < 0) {
        throw new NumberFormatException("illegal number");
    }
    if (result < multmin) {
        throw new NumberFormatException("illegal number");
    }
    result *= radix;
    if (result < limit + digit) {
        throw new NumberFormatException("illegal number");
    }
    result -= digit;
      }
  } else {
      throw new NumberFormatException("illegal number");
  }
  if (negative) {
      if (i > start + 1) {
    return result;
      } else {  /* Only got "-" */
    throw new NumberFormatException("illegal number");
      }
  } else {
      return -result;
  }
    }
    /**
     * Convert the bytes within the specified range of the given byte 
     * array into a signed integer . The range extends from 
     * <code>start</code> till, but not including <code>end</code>. <p>
     */
    public static int parseInt(byte[] b, int start, int end)
    throws NumberFormatException {
  return parseInt(b, start, end, 10);
    }
    /**
     * Convert the bytes within the specified range of the given byte 
     * array into a signed long in the given radix . The range extends 
     * from <code>start</code> till, but not including <code>end</code>. <p>
     *
     * Based on java.lang.Long.parseLong()
     */
    public static long parseLong(byte[] b, int start, int end, int radix)
    throws NumberFormatException {
  if (b == null)
      throw new NumberFormatException("null");
  
  long result = 0;
  boolean negative = false;
  int i = start;
  long limit;
  long multmin;
  int digit;
  if (end > start) {
      if (b[i] == "-") {
    negative = true;
    limit = Long.MIN_VALUE;
    i++;
      } else {
    limit = -Long.MAX_VALUE;
      }
      multmin = limit / radix;
      if (i < end) {
    digit = Character.digit((char)b[i++], radix);
    if (digit < 0) {
        throw new NumberFormatException(
      "illegal number: " + toString(b, start, end)
      );
    } else {
        result = -digit;
    }
      }
      while (i < end) {
    // Accumulating negatively avoids surprises near MAX_VALUE
    digit = Character.digit((char)b[i++], radix);
    if (digit < 0) {
        throw new NumberFormatException("illegal number");
    }
    if (result < multmin) {
        throw new NumberFormatException("illegal number");
    }
    result *= radix;
    if (result < limit + digit) {
        throw new NumberFormatException("illegal number");
    }
    result -= digit;
      }
  } else {
      throw new NumberFormatException("illegal number");
  }
  if (negative) {
      if (i > start + 1) {
    return result;
      } else {  /* Only got "-" */
    throw new NumberFormatException("illegal number");
      }
  } else {
      return -result;
  }
    }
    /**
     * Convert the bytes within the specified range of the given byte 
     * array into a signed long . The range extends from 
     * <code>start</code> till, but not including <code>end</code>. <p>
     */
    public static long parseLong(byte[] b, int start, int end)
    throws NumberFormatException {
  return parseLong(b, start, end, 10);
    }
    /**
     * Convert the bytes within the specified range of the given byte 
     * array into a String. The range extends from <code>start</code>
     * till, but not including <code>end</code>. <p>
     */
    public static String toString(byte[] b, int start, int end) {
  int size = end - start;
  char[] theChars = new char[size];
  for (int i = 0, j = start; i < size; )
      theChars[i++] = (char)(b[j++]&0xff);
  
  return new String(theChars);
    }
    public static String toString(ByteArrayInputStream is) {
  int size = is.available();
  char[] theChars = new char[size];
  byte[] bytes    = new byte[size];
  is.read(bytes, 0, size);
  for (int i = 0; i < size;)
      theChars[i] = (char)(bytes[i++]&0xff);
  
  return new String(theChars);
    }

    public static byte[] getBytes(String s) {
  char [] chars= s.toCharArray();
  int size = chars.length;
  byte[] bytes = new byte[size];
      
  for (int i = 0; i < size;)
      bytes[i] = (byte) chars[i++];
  return bytes;
    }
    public static byte[] getBytes(InputStream is) throws IOException {
  int len;
  int size = 1024;
  byte [] buf;

  if (is instanceof ByteArrayInputStream) {
      size = is.available();
      buf = new byte[size];
      len = is.read(buf, 0, size);
  }
  else {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      buf = new byte[size];
      while ((len = is.read(buf, 0, size)) != -1)
    bos.write(buf, 0, len);
      buf = bos.toByteArray();
  }
  return buf;
    }
}





Display the ISO Latin-1 character set

  
public class LatinSet {
  public static void main(String[] args) {
    System.out.println("ISO LATIN-1 CHARACTER SET");
    for (int x = 160; x < 255; x++) {
      System.out.print((char) x);
      System.out.print("  ");
      if (x % 8 == 7)
        System.out.print("\n");
    }
  }
}





Reading CRLF terminated lines that contain only US-ASCII characters from an input stream

 

import java.io.*;
/**
 * This class is to support reading CRLF terminated lines that
 * contain only US-ASCII characters from an input stream. Provides
 * functionality that is similar to the deprecated 
 * <code>DataInputStream.readLine()</code>. Expected use is to read
 * lines as String objects from a RFC822 stream.
 *
 * It is implemented as a FilterInputStream, so one can just wrap 
 * this class around any input stream and read bytes from this filter.
 * 
 * @author John Mani
 */
public class LineInputStream extends FilterInputStream {
    private char[] lineBuffer = null; // reusable byte buffer
    public LineInputStream(InputStream in) {
  super(in);
    }
    /**
     * Read a line containing only ASCII characters from the input 
     * stream. A line is terminated by a CR or NL or CR-NL sequence.
     * A common error is a CR-CR-NL sequence, which will also terminate
     * a line.
     * The line terminator is not returned as part of the returned 
     * String. Returns null if no data is available. <p>
     *
     * This class is similar to the deprecated 
     * <code>DataInputStream.readLine()</code>
     */
    public String readLine() throws IOException {
  InputStream in = this.in;
  char[] buf = lineBuffer;
  if (buf == null)
      buf = lineBuffer = new char[128];
  int c1;
  int room = buf.length;
  int offset = 0;
  while ((c1 = in.read()) != -1) {
      if (c1 == "\n") // Got NL, outa here.
    break;
      else if (c1 == "\r") {
    // Got CR, is the next char NL ?
    int c2 = in.read();
    if (c2 == "\r")   // discard extraneous CR
        c2 = in.read();
    if (c2 != "\n") {
        // If not NL, push it back
        if (!(in instanceof PushbackInputStream))
      in = this.in = new PushbackInputStream(in);
        ((PushbackInputStream)in).unread(c2);
    }
    break; // outa here.
      }
      // Not CR, NL or CR-NL ...
      // .. Insert the byte into our byte buffer
      if (--room < 0) { // No room, need to grow.
    buf = new char[offset + 128];
    room = buf.length - offset - 1;
    System.arraycopy(lineBuffer, 0, buf, 0, offset);
    lineBuffer = buf;
      }
      buf[offset++] = (char)c1;
  }
  if ((c1 == -1) && (offset == 0))
      return null;
  
  return String.copyValueOf(buf, 0, offset);
    }
}