Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]
    1   //
    2   //  Neuros Database Manipulator
    3   //  Copyright (C) 2003  Neuros Database Manipulator
    4   //
    5   //  This program is free software; you can redistribute it and/or modify
    6   //  it under the terms of the GNU General Public License as published by
    7   //  the Free Software Foundation; either version 2 of the License, or
    8   //  (at your option) any later version.
    9   //
   10   //  This program is distributed in the hope that it will be useful,
   11   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13   //  GNU General Public License for more details.
   14   //
   15   //  You should have received a copy of the GNU General Public License
   16   //  along with this program; if not, write to the Free Software
   17   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18   //
   19   //  For information about Neuros Database Manipulator and its authors,
   20   //  please contact the Neuros Database Manipulator Web Site at
   21   //  http://neurosdbm.sourceforge.net
   22   //
   23   //
   24   
   25   package net.sourceforge.neurosdbm.db;
   26   
   27   
   28   import java.util.ArrayList;
   29   import java.util.Iterator;
   30   import java.io.IOException;
   31   
   32   public class WordString {
   33     protected byte[] bytes;
   34     private int wordLength;
   35   
   36     public WordString(byte[] in_bytes, int wordOffset) {
   37       int start = wordOffset*2;
   38       int byteLength = 0;
   39       while (in_bytes[start+byteLength] != 0) {
   40         byteLength++;
   41       }
   42       bytes = new byte[byteLength];
   43       for (int cnt=0; cnt<byteLength; cnt++) {
   44         bytes[cnt] = in_bytes[start+cnt];
   45       }
   46   
   47       wordLength = 0;
   48       if ((byteLength % 2) == 0) {
   49         wordLength = (byteLength/2) + 1;
   50       } else {
   51         wordLength = ((byteLength+1)/2) + 1;
   52       }
   53     }
   54   
   55     
   56     public WordString(String value) {
   57       bytes = value.getBytes();
   58       wordLength = bytes.length/2;
   59       if ((bytes.length % 2) == 1) {
   60         wordLength++;
   61       }
   62       wordLength++;
   63     }
   64     
   65   
   66     public WordString(byte[] bytes, WordPointer wordPointer) {
   67       this(bytes, wordPointer.getValue());
   68     }
   69   
   70   
   71     public WordString(ExtraInfo ei) {
   72       StringBuffer buffer = new StringBuffer();
   73       wordLength = 0;
   74       boolean done = false;
   75       for (Iterator iter = ei.getWordArrayList().iterator(); iter.hasNext() ;) {
   76         Word word = (Word) iter.next();
   77         if (!done) {
   78           if (word.getMSB() != 0x00) {
   79             buffer.append((char)word.getMSB());
   80           } else {
   81             done = true;
   82           }
   83           if (word.getLSB() != 0x00) {
   84             buffer.append((char)word.getLSB());
   85           } else {
   86             done = true;
   87           }
   88         }
   89         wordLength++;
   90       }
   91       bytes = buffer.toString().getBytes();
   92     }
   93   
   94     ExtraInfo createExtraInfo() {
   95       byte[] out_bytes = new byte[2*(((bytes.length+1) / 2) + 1)];
   96       int pos = 0;
   97       for (int cnt=0; cnt<bytes.length; cnt++) {
   98         out_bytes[pos] = bytes[cnt];
   99         pos++;
  100       }
  101       if ((bytes.length % 2) == 1) {
  102         out_bytes[pos] = 0x00;
  103         pos++;
  104       }
  105       out_bytes[pos] = 0x00;
  106       pos++;
  107       out_bytes[pos] = 0x00;
  108       pos++;
  109       ExtraInfo ei = new ExtraInfo(out_bytes);
  110       ei.setType(ExtraInfo.TYPE_WORDSTRING);
  111       return ei;
  112     }
  113   
  114     
  115     // Length of the string, including the null word at the end
  116     public int getWordLength() {
  117       return wordLength;
  118     }
  119   
  120     
  121     public void write(NIOFile file) throws IOException {
  122       file.append(bytes);
  123       if ((bytes.length % 2) == 1) {
  124         file.append(0x00);
  125       }
  126       file.append(0x00);
  127       file.append(0x00);
  128     }
  129   
  130     
  131     public String toString() {
  132       return new String(bytes);
  133     }
  134   }

Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]