⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbfnet.java

📁 geotools的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package uk.ac.leeds.ccg.dbffile;

import java.io.*;
import java.util.*;
import java.lang.*;
import java.net.*;
import cmp.LEDataStream.*;

/** 
 * 
 * This class represents a DBF (or DBase) file.<p>
 * Construct it with a URL (including the .dbf) 
 * this causes the header and field definitions to be read.<p>
 * Later queries return rows or columns of the database.
 *<hr>
 * @author <a href="mailto:ian@geog.leeds.ac.uk">Ian Turton</a> Centre for
 * Computaional Geography, University of Leeds, LS2 9JT, 1998.
 * <br>
 * mod to getStringCol by James Macgill.
 */
public final class DbfNet implements DbfConsts{
    static final boolean DEBUG=true;
    int dbf_id;
    int last_update_d,last_update_m,last_update_y;
    int last_rec;
    int data_offset;
    int rec_size;
		StringBuffer records[];
		int position=0;
    boolean hasmemo;
    LEDataInputStream dFile;
    int filesize,numfields;
    public DbfFieldDef fielddef[];

		/** 
			* Constructor, opens the file and reads the header infomation.
			* @param file The file to be opened, includes path and .dbf
			* @exception java.io.IOException If the file can't be opened.
			* @exception DbfFileException If there is an error reading header.
			*/
	public DbfNet(URL url) throws java.io.IOException,DbfFileException{
		if(DEBUG)System.out.println("---->uk.ac.leeds.ccg.dbffile.DbfNet constructed. Will identify itself as DbN->");
		URLConnection uc = url.openConnection();
		InputStream in = uc.getInputStream();
		LEDataInputStream sfile = new LEDataInputStream(in);
		init(sfile);
	}

	public DbfNet(InputStream in) throws java.io.IOException,DbfFileException{
		if(DEBUG)System.out.println("---->uk.ac.leeds.ccg.dbffile.DbfNet constructed. Will identify itself as DbN->");
		LEDataInputStream sfile = new LEDataInputStream(in);
		init(sfile);
    }

	public DbfNet(String name) throws java.io.IOException,DbfFileException{
		if(DEBUG)System.out.println("---->uk.ac.leeds.ccg.dbffile.DbfNet constructed. Will identify itself as DbN->");
		URL url = new URL(name);
		URLConnection uc = url.openConnection();
		InputStream in = uc.getInputStream();
		LEDataInputStream sfile = new LEDataInputStream(in);
		init(sfile);
	}
		/**
			* Returns the date of the last update of the file as a string.
			*/
	public String getLastUpdate(){
		String date=last_update_d+"/"+last_update_m+"/"+last_update_y;
		return date;
	}

		/**
			* Returns the number of records in the database file.
			*/
	public int getLastRec(){
		return last_rec;
	}
		/**
			* Returns the size of the records in the database file.
			*/
	public int getRecSize(){
		return  rec_size;
	}
		/**
			* Returns the number of fields in the records in the database file.
			*/
    public int getNumFields(){
      return numfields;
    }

    /**
     * looks up the field number for the given named column
     * @param name A String for the name to look up
     * @return int The col number for the field, -1 if field could not be found
     */
	public int getFieldNumber(String name){
		for(int i=0;i<numfields;i++){
			//System.out.println(i);
			// System.out.println(fielddef[i].fieldname.toString()+" "+name);
			if(name.trim().equalsIgnoreCase(fielddef[i].fieldname.toString().trim())){
				return i;
			}
		}
		return -1;//not found
	}
		/**
			* Returns the size  of the database file.
			*/
	public int getFileSize(){
		return  filesize;
	}
	public StringBuffer getFieldName(int col){
		if(fielddef.length - 1 < col) throw new IllegalArgumentException("DbN->column number specified is invalid. It's higher than the amount of columns available");
		return  fielddef[col].fieldname;
	}
	public char getFieldType(int col){
		if(fielddef.length - 1 < col) throw new IllegalArgumentException("DbN->column number specified is invalid. It's higher than the amount of columns available");
		return fielddef[col].fieldtype;
	}

		/**
		  * initailizer, allows the use of multiple constructers in later
			* versions.
			*/

    private void init(LEDataInputStream sfile)throws
      IOException,DbfFileException {
      DbfFileHeader head = new DbfFileHeader(sfile);
      int widthsofar;

      dFile=sfile;

      fielddef = new DbfFieldDef[numfields];
      widthsofar=1;
      for(int index=0;index<numfields;index++){
        fielddef[index] = new DbfFieldDef(widthsofar);
        widthsofar+=fielddef[index].fieldlen;
      }
      //System.out.println("Skippint two bytes");
      sfile.skipBytes(1); // end of field defs marker
			records=GrabFile();
    }
	/**
		* Internal Class to hold information from the header of the file
		*/
  final class DbfFileHeader{


		/**
			* Reads the header of a dbf file.
			* @param LEDataInputStream file Stream attached to the input file
			* @exception IOException read error.
			*/
    public  DbfFileHeader(LEDataInputStream file) throws IOException {
      getDbfFileHeader(file);
    }
    private void getDbfFileHeader(LEDataInputStream file) throws IOException {

      int len;
      dbf_id=(int)file.readUnsignedByte();
      if(DEBUG)System.out.print("DbN->Header id ");
      if(DEBUG)System.out.println(dbf_id);
      if(dbf_id==3) hasmemo=true;
      else hasmemo=false;

      last_update_y=(int)file.readUnsignedByte();
      last_update_m=(int)file.readUnsignedByte();
      last_update_d=(int)file.readUnsignedByte();
      if(DEBUG)System.out.print("DbN->last update ");
      if(DEBUG)System.out.print(last_update_d);
      if(DEBUG)System.out.print("/");
      if(DEBUG)System.out.print(last_update_m);
      if(DEBUG)System.out.print("/");
      if(DEBUG)System.out.println(last_update_y);

      last_rec=file.readInt();
      if(DEBUG)System.out.print("DbN->last rec ");
      if(DEBUG)System.out.println(last_rec);

      data_offset=file.readShort();
			//data_offset=0;
			//System.out.println("x = "+file.readUnsignedByte()+" " +
				//file.readUnsignedByte());
      if(DEBUG)System.out.print("DbN->data offset ");
      if(DEBUG)System.out.println(data_offset);

      rec_size=file.readShort();
      if(DEBUG)System.out.print("DbN->rec_size ");
      if(DEBUG)System.out.println(rec_size);

      filesize=(rec_size * last_rec) + data_offset+1;
      numfields = (data_offset - DBF_BUFFSIZE -1)/DBF_BUFFSIZE;

      if(DEBUG)System.out.print("DbN->num fields ");
      if(DEBUG)System.out.println(numfields);
      if(DEBUG)System.out.print("DbN->file size ");
      if(DEBUG)System.out.println(filesize);
      file.skipBytes(20);
    }

  }
	/**
		* Internal class to hold infomation about the fields in the file
		*/
  final class DbfFieldDef{
    public StringBuffer fieldname = new StringBuffer(DBF_NAMELEN);
    public char fieldtype;
    public int  fieldstart;
    public int  fieldlen;
    public int  fieldnumdec;
    DbfFieldDef(int pos) throws IOException {
      byte[] strbuf = new byte[DBF_NAMELEN]; // <---- byte array buffer fo storing string's byte data
      int j=-1;
			int term =-1;
      for(int i=0;i<DBF_NAMELEN;i++){
        byte b = dFile.readByte();
        if(b==0){
					if(term== -1 )
						term=j;
					continue;
				}
        j++;
        strbuf[j] = b; // <---- read string's byte data
      }
			if(term==-1) term=j;
      String name = new String(strbuf, 0, term+1);
      if(DEBUG)System.out.println("DbN->Loaded as "+name);
      fieldname.append(name); // <- append byte array to String Buffer
      if(DEBUG)System.out.println("DbN->Fieldname "+fieldname);
      fieldtype=(char)dFile.readUnsignedByte();
      fieldstart=pos;
      dFile.skipBytes(4);
      switch(fieldtype){
        case 'C':
        case 'c':
        case 'L':
        case 'M':
        case 'G':
        case 'N':
        case 'n':
          fieldlen=(int)dFile.readUnsignedByte();
          fieldnumdec=(int)dFile.readUnsignedByte();
          fieldnumdec=0;
          break;
        case 'F':
        case 'f':
          fieldlen=(int)dFile.readUnsignedByte();
          fieldnumdec=(int)dFile.readUnsignedByte();
          break;
        default:
          System.out.println("DbN->Help - wrong field type: "+fieldtype);
        }
      if(DEBUG)System.out.println("DbN->Fieldtype "+fieldtype+" width "+fieldlen+
        "."+fieldnumdec);

      dFile.skipBytes(14);


    }
  }
	/**
		* gets the next record and returns it as a string. This method works on
		* a sequential stream and can not go backwards. Only useful if you want
		* to read the whole file in one.
		* @exception java.io.IOException on read error.
		*/
  public  StringBuffer GetNextDbfRec()throws java.io.IOException{
		return records[position++];
	}

  private StringBuffer GrabNextDbfRec()throws java.io.IOException{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -