📄 zlib.java
字号:
// zlib.java zilla // modified// apr02 min/max integer// mar01 bull// feb01 scruff// jan01 bull// oct00 bull// jun00 scruff// may00 scruff// apr00 scruff// jan99 doom// jan97 doom: created// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Library General Public// License as published by the Free Software Foundation; either// version 2 of the License, or (at your option) any later version.// // This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Library General Public License for more details.// // You should have received a copy of the GNU Library General Public// License along with this library; if not, write to the// Free Software Foundation, Inc., 59 Temple Place - Suite 330,// Boston, MA 02111-1307, USA.//// contact info: zilla@computer.orgpackage zlib;import java.io.*;import java.util.*;import javax.swing.*;import java.awt.*;/** * zlib - misc utilities * * @author jplewis */final public class zlib{ /** * write string bytes on a stream */ public static void writeString(BufferedOutputStream f, String s) throws IOException { //s.getBytes(0, s.length(), buf, 0); //deprecated byte[] buf = s.getBytes(); f.write(buf, 0, s.length()); } //writeString /** * read the whole input stream into a string (beware) */ public static String InputStreamToString(BufferedReader fr) throws IOException { StringBuffer s = new StringBuffer(); String line = fr.readLine(); while( line != null ) { s.append(line); line = fr.readLine(); } fr.close(); return s.toString(); } /** * read the input stream through the next newline, then stop. */ public static void InputStreamEatLine(InputStream f) throws IOException { int cc; do { cc = f.read(); } while( cc != '\n' ); } //InputStreamEatLine /** * Provide a readline method for an input stream. * (BufferedReader has a readLine method.) * Useful for reading binary ppm files - read the header in * a line-oriented way, then read binary bytes. * This method allocates a byte[] array each time. Beware. */ public static String InputStreamReadLine(InputStream f) throws IOException { byte[] buf = new byte[2048]; // TODO: garbage int cc; int i = 0; do { cc = f.read(); if (cc != '\n') { buf[i] = (byte)cc; i++; if (i == 2048) break; } } while( cc != '\n' ); return new String(buf, 0, i); } //InputStreamReadLine /** * read a token, stop at newline, tab, space */ public static String InputStreamReadToken(InputStream f, String whitechars) throws IOException { byte[] buf = new byte[2048]; // TODO: garbage int cc; int i = 0; boolean iswhite = false; do { cc = f.read(); if (cc == -1) break; iswhite = (whitechars.indexOf(cc) >= 0); if (!iswhite) { buf[i] = (byte)cc; i++; if (i == 2048) break; } } while(!iswhite); return new String(buf, 0, i); } //InputStreamReadToken /** * read the next token, skipping comment line * this is primarly for ppm parsing * NOT DEBUGGED */ public static String InputStreamReadTokenComment(InputStream f, String whitechars, char commentchar) throws IOException { byte[] buf = new byte[2048]; // TODO: garbage int cc; int i = 0; boolean iswhite = false; do { cc = f.read(); if (cc == commentchar) { while( (cc != '\n') && (cc != '\r') && (cc != -1) ) { cc = f.read(); } if (i > 0) // token already started break; else { if (cc != -1) cc = f.read(); } } if (cc == -1) break; iswhite = (whitechars.indexOf(cc) >= 0); if (!iswhite) { buf[i] = (byte)cc; i++; if (i == 2048) break; } } while(!iswhite); return new String(buf, 0, i); } //InputStreamReadToken //---------------------------------------------------------------- /** <pre> state table for streamtokenizer: Input Action New state ---------------- State=idle ---------------- word character push back character accumulate ordinary character return character idle whitespace character consume character idle ---------------- State=accumulate ---------------- word character add to current word accumulate ordinary character return current word idle push back character whitespace character return current word idle consume character for jdk1.4.2: TT_EOF = -1 TT_EOL = -10 TT_WRD = -3 TT_NUM = -2 and ordinary (non word) characters are returned as their numberic value */ /** * handy setup a streamtokenizer */ public static StreamTokenizer getParsingStream(String file, String whitechars, String wordchars, String commentchars, String breakchars, boolean eolIsSignificant, boolean lowercase) throws IOException { return getParsingStream(file, whitechars,wordchars, commentchars,breakchars, eolIsSignificant,lowercase, false); } /** * I think breakchars are things like ; that should be returned * as separate tokens. * Typical call: <pre> StreamTokenizer st = zlib.getParsingStream(cmdline[0], " \t\r\n", "'", "", "", false,true, false); */ public static StreamTokenizer getParsingStream(String file, String whitechars, String wordchars, String commentchars, String breakchars, boolean eolIsSignificant, boolean lowercase, boolean doNumbers) throws IOException { BufferedReader br = new BufferedReader(new FileReader(file)); StreamTokenizer st = new StreamTokenizer(br); st.resetSyntax(); if (doNumbers) st.parseNumbers(); st.wordChars((int)'A',(int)'Z'); st.wordChars((int)'a',(int)'z'); //st.whitespaceChars((int)'\u0000',(int)'\u0020'); char[] arr = whitechars.toCharArray(); for( int i = 0; i < arr.length; i++ ) st.whitespaceChars(arr[i],arr[i]); arr = wordchars.toCharArray(); for( int i = 0; i < arr.length; i++ ) st.wordChars(arr[i],arr[i]); arr = commentchars.toCharArray(); for( int i = 0; i < arr.length; i++ ) st.commentChar(arr[i]); arr = breakchars.toCharArray(); for( int i = 0; i < arr.length; i++ ) // note 'ordinary' as opposed to 'word' st.ordinaryChars(arr[i],arr[i]); st.eolIsSignificant(eolIsSignificant); //System.out.println("eolIsSignificant = "+eolIsSignificant); st.lowerCaseMode(lowercase); return st; } //getParsingStream /** * print the current StreamTokenizer token */ public static void printToken(StreamTokenizer st) { switch(st.ttype) { case StreamTokenizer.TT_WORD: System.out.print(st.sval); break; case StreamTokenizer.TT_NUMBER: System.out.print(st.nval); break; case StreamTokenizer.TT_EOL: System.out.print("<eol>"); break; case StreamTokenizer.TT_EOF: System.out.print("<eof>"); break; default: System.out.print("character("+st.ttype+")"); } } //printToken /** * read the next token from st, * call parseError if it is not 'desired' */ public static void parseToken(StreamTokenizer st, String desired, String file) throws IOException { int val = st.nextToken(); if (val != StreamTokenizer.TT_WORD) zliberror.parseError(st, file); if (!st.sval.equals(desired)) zliberror.parseError(st, file); } //parseToken /** * read the next token from st, * call parseError if it is not a string, else return it. * (jul05 added) exception: if EOF return null */ public static String parseString(StreamTokenizer st, String file) throws IOException { int val = st.nextToken(); //System.out.println("val = "+val); if (val == StreamTokenizer.TT_EOF) return null; if (val != StreamTokenizer.TT_WORD) zliberror.parseError(st, file); return st.sval; } //parseString /** * Debugging version of parseString - * prints out all the internal variables. */ public static String parseStringDebug(StreamTokenizer st, String file) throws IOException { int val = st.nextToken(); System.out.println("parseStringDebug val = " + val); System.out.println(" TT_WORD = " + StreamTokenizer.TT_WORD); System.out.println(" TT_NUMBER = " + StreamTokenizer.TT_NUMBER); System.out.println(" TT_EOL = " + StreamTokenizer.TT_EOL); System.out.println(" TT_EOF = " + StreamTokenizer.TT_EOF); System.out.println(" nval = " + st.nval); System.out.println(" sval = " + st.sval); if (val != StreamTokenizer.TT_WORD) zliberror.parseError(st, file); return st.sval; } //parseStringDebug /** * read the next token from st, * call parseError if it is not a double, else return it * Beware: if the input has E notation numbers, this wont * work, it is necessary to do: * <pre> * st.resetSyntax(); * st.eolIsSignificant(false); * st.whitespaceChars((int)'\u0000',(int)'\u0020'); * st.wordChars((int)'0',(int)'9'); * st.wordChars((int)'.',(int)'.'); * st.wordChars((int)'E',(int)'E'); * st.wordChars((int)'-',(int)'-'); * st.wordChars((int)'+',(int)'+'); * //st.commentChar('#'); * </pre> */ public static double parseDouble(StreamTokenizer st, String file) throws IOException { int val = st.nextToken(); if (val != StreamTokenizer.TT_NUMBER) zliberror.parseError(st, file); return st.nval; } //parseDouble //---------------------------------------------------------------- /** */ public static String getFilename(String path) { File f = new File(path); return f.getName(); } /** */ public static String getDirectory(String path) { File f = new File(path); return f.getParent(); } /** * return the root part of a pathname (excluding the period). * null if no such. */ public static String getRoot(String path) { int i = path.lastIndexOf('.'); if (i == -1) return path; return path.substring(0,i); } /** * return the .ext part of a pathname (including the period). * null if no such. */ public static String getExtension(String path) { int i = path.lastIndexOf('.'); if (i == -1) return null; return path.substring(i); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -