1 using System;
  2 using System.Collections;
  3 using System.Text;
  4 using System.IO;
  5 using System.Reflection;
  6 
  7 namespace org.xmlBlaster.util
  8 {
  9    public class FileLocator
 10    {
 11       public static byte[] getFileAsBytes(string fileName) {
 12          BinaryReader sReader = null;
 13          byte[] contents = null;
 14          try {
 15             FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
 16             //sReader = new BinaryReader(fileStream);

 17             contents = ReadFully(fileStream, 0);
 18          }
 19          finally {
 20             if (sReader != null) {
 21                sReader.Close();
 22             }
 23          }
 24          return contents;
 25       }
 26 
 27       public static FileContainer[] GetFiles(string directory, string fileName) {
 28          string[] fileNames = Directory.GetFiles(directory, fileName);
 29          FileContainer[] fileContainers = new FileContainer[fileNames.Length];
 30          for (int i = 0; i < fileNames.Length; i++) {
 31             fileContainers[i] = new FileContainer(directory, fileNames[i]);
 32          }
 33          return fileContainers;
 34       }
 35 
 36       public static type[] GetObjectsFromFiles<type>(string directory, string fileName) {
 37          FileContainer[] ff = GetFiles(directory, fileName);
 38          type[] types = new type[ff.Length];
 39          for (int i=0; i<types.Length; i++) {
 40             types[i] = ff[i].Deserialize<type>();
 41          }
 42          return types;
 43       }
 44 
 45       public static byte[] ReadFully(Stream stream, int initialLength) {
 46          // If we've been passed an unhelpful initial length, just

 47          // use 32K.

 48          if (initialLength < 1) {
 49             initialLength = 32768;
 50          }
 51 
 52          byte[] buffer = new byte[initialLength];
 53          int read = 0;
 54 
 55          int chunk;
 56          while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0) {
 57             read += chunk;
 58 
 59             // If we've reached the end of our buffer, check to see if there's

 60             // any more information

 61             if (read == buffer.Length) {
 62                int nextByte = stream.ReadByte();
 63 
 64                // End of stream? If so, we're done

 65                if (nextByte == -1) {
 66                   return buffer;
 67                }
 68 
 69                // Nope. Resize the buffer, put in the byte we've just

 70                // read, and continue

 71                byte[] newBuffer = new byte[buffer.Length * 2];
 72                Array.Copy(buffer, newBuffer, buffer.Length);
 73                newBuffer[read] = (byte)nextByte;
 74                buffer = newBuffer;
 75                read++;
 76             }
 77          }
 78          // Buffer is now too big. Shrink it.

 79          byte[] ret = new byte[read];
 80          Array.Copy(buffer, ret, read);
 81          return ret;
 82       }
 83 
 84 
 85       public static string getFileAsString(string fileName)
 86       {
 87          StreamReader sReader = null;
 88          string contents = null;
 89          try
 90          {
 91             FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
 92             sReader = new StreamReader(fileStream);
 93             contents = sReader.ReadToEnd();
 94          }
 95          finally
 96          {
 97             if (sReader != null)
 98             {
 99                sReader.Close();
100             }
101          }
102          return contents;
103       }
104 
105       /// <summary>

106       /// Write a file

107       /// </summary>

108       /// <param name="fileName">"c:\\tmp\\WriteFileStuff.txt"</param>

109       /// <param name="text">The data to write</param>

110       public static void writeAsciiFile(string fileName, string text)
111       {
112          FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
113          StreamWriter sw = new StreamWriter(fs);
114          try
115          {
116             sw.Write(text);
117          }
118          finally
119          {
120             if (sw != null) { sw.Close(); }
121          }
122       }
123 
124       public static bool moveFile(string origName, string destName, bool overwrite)
125       {
126          try
127          {
128             if (File.Exists(destName))
129             {
130                if (!overwrite) return false;
131                File.Delete(destName);
132             }
133             if (File.Exists(origName))
134             {
135                File.Move(origName, destName);
136                return true;
137             }
138          }
139          catch (Exception ex)
140          {
141             Console.WriteLine("Move '" + origName + "' to '" + destName + "' failed: " + ex.ToString());
142          }
143          return false;
144       }
145 
146       /*
147       public static byte[] readBinaryFile(string fileName)
148       {
149          if (!File.Exists(fileName))
150             return new byte[0];
151          FileStream fs = File.OpenRead(fileName);
152          if (fs == null) return new byte[0];
153          BinaryReader br = null;
154          try {
155             br = new BinaryReader(fs);
156             if (br == null) return new byte[0];
157             // TODO!! loop until complete file is read
158             byte[] bytes = br.ReadBytes(10);
159             return bytes;
160          }
161          finally {
162             if (br != null) br.Close();
163             fs.Close();
164          }
165       }
166       */
167 
168       /// <summary>

169       /// Write a file

170       /// </summary>

171       /// <param name="fileName">"c:\\tmp\\WriteFileStuff.txt"</param>

172       /// <param name="text">The data to write</param>

173       public static bool writeBinaryFile(string fileName, byte[] text)
174       {
175          //FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);

176          FileStream fs = File.Create(fileName);
177          if (fs == null) return false;
178          try
179          {
180             BinaryWriter bw = new BinaryWriter(fs);
181             bw.Write(text);
182             bw.Close();
183             return true;
184          }
185          finally
186          {
187             fs.Close();
188          }
189       }
190    } // FileLocator

191 
192    public class FileContainer {
193       string directoy;
194       string fileName;
195 
196       public FileContainer(string d, string f) {
197          this.directoy = d;
198          this.fileName = f;
199       }
200 
201        public string Directory
202        {
203            get { return directoy; }
204            set { directoy = value; }
205        }
206 
207       public string FileName {
208          get { return fileName; }
209          set { fileName = value; }
210       }
211 
212       byte[] xmlContent;
213 
214       public byte[] GetXmlContent() {
215          if (xmlContent == null) {
216             string name = Path.Combine(this.directoy, this.fileName);
217             if (name != null)
218                xmlContent = org.xmlBlaster.util.FileLocator.getFileAsBytes(name);
219          }
220          if (xmlContent == null) return new byte[0];
221          return xmlContent;
222       }
223 
224       public string GetXmlContentUtf8Str() {
225          return org.xmlBlaster.util.Serialization.UTF8ByteArrayToString(GetXmlContent());
226       }
227 
228       public type Deserialize<type>() {
229          return org.xmlBlaster.util.Serialization.Deserialize<type>(GetXmlContent());
230       }
231    } // FileContainer

232 }


syntax highlighted by Code2HTML, v. 0.9.1