static void get(String srcFileOnAqua, String destFileOnLocal) { // step 1. instantiate a client to connect to Aqua String rootURL = "http://*****:*****@172.16.20.131:8080/aqua/rest/cdmi"; String homeContainer = "/hongquan/test999"; String jsonProps = ""; AquaClientRef client = new AquaClientRef(rootURL, homeContainer, jsonProps); try { // step 2. open the output file on local disk to save FileStream fcOut = null; Console.WriteLine("opening external output file: " + destFileOnLocal); fcOut = new FileStream(destFileOnLocal, FileMode.Create); // step 3. allocate a Byte array dataBuf. be aware this buffer must be allocated via direct mode int buffSize = 100 * 1024; byte[] dataBuf = new byte[buffSize]; // step 4. copy the data buffer by buffer and save to the local file int len = 0; int offset = 0; // the output string parameters from API nonCdmi_ReadDataObject() StringBuilder location = new StringBuilder(); StringBuilder contentType = new StringBuilder(); // the loop do { // step 4.1 clean up the data left in the dataBuf Array.Clear(dataBuf, 0, dataBuf.Length); // step 4.2 initialize the output nread with the capacity of dataBuf len = dataBuf.Length; LongVariable nread = new LongVariable(0); // step 4.3 invokes nonCdmi_ReadDataObject() to read data range [offset, offset+dataBuf.capacity()] into dataBuf int ret = client.nonCdmi_ReadDataObject(srcFileOnAqua, contentType, location, offset, nread, dataBuf, len, false); Console.WriteLine("read aqua contenttype=" + contentType + " location=" + location); // step 4.4 check the return code if (ret >= 200 && ret < 300) { len = (int)nread.get(); } else { len = 0; } Console.WriteLine("read aqua ret=" + ret + " len=" + len); // step 4.5 quit the loop if error occurs or reached the EOF if (len <= 0) { break; } // step 4.6 write the received dataBuf into the local file Console.WriteLine("copying " + offset + "+" + len); fcOut.Write(dataBuf, 0, len); //setp4.7 file eof // if(len < dataBuf.Length) // break; // step 4.7 step the offset for next round offset += len; } while (len > 0); // step 5. finish getting, close the local file Console.WriteLine("file copied, size=" + offset); fcOut.Close(); } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message + " in the specified directory."); } catch (IOException e) { Console.WriteLine(e.Message); } }
static void copy(String srcFile, String destFile) { bool bFromAqua = (0 == srcFile.IndexOf("FUSE:")); bool bToAqua = (0 == destFile.IndexOf("FUSE:")); String rootURL = "http://*****:*****@172.16.20.131:8080/aqua/rest/cdmi"; String homeContainer = "/default/npvr"; String jsonProps = ""; AquaClientRef client = new AquaClientRef(rootURL, homeContainer, jsonProps); try { FileStream fcIn = null, fcOut = null; if (!bFromAqua) { Console.WriteLine("opening external input file: " + srcFile); fcIn = new FileStream(srcFile, FileMode.Open); } else { srcFile = srcFile.Substring(5); // cut off the leading "FUSE:" } if (!bToAqua) { Console.WriteLine("opening external output file: " + destFile); fcOut = new FileStream(srcFile, FileMode.Create); } else { destFile = destFile.Substring(5); // cut off the leading "FUSE:" } int buffSize = 100 * 1024; byte[] dataBuf = new byte[buffSize]; int len = 0; int offset = 0; StringBuilder location = new StringBuilder(); StringBuilder contentType = new StringBuilder(); do { Array.Clear(dataBuf, 0, dataBuf.Length); if (bFromAqua) { len = dataBuf.Length; LongVariable nread = new LongVariable(0); int ret = client.nonCdmi_ReadDataObject(srcFile, contentType, location, offset, nread, dataBuf, len, false); if (ret >= 200 && ret < 300) { len = (int)nread.get(); } Console.WriteLine("read aqua ret=" + ret + " len=" + len); } else { // len = fis.read(dataBuf); len = fcIn.Read(dataBuf, 0, buffSize); } if (len <= 0) { break; } Console.WriteLine("copying " + offset + "+" + len); if (bToAqua) { int ret = client.nonCdmi_UpdateDataObject(destFile, location, "", offset, -1, dataBuf, len, true, false); if (ret < 200 || ret >= 300) { break; } } else { // fos.write(dataBuf, 0, len); fcOut.Write(dataBuf, 0, len); } offset += len; } while (len > 0); Console.WriteLine("file copied, size=" + offset); if (!bFromAqua) { // fis.close(); fcIn.Close(); } if (!bToAqua) { // fos.close(); fcOut.Close(); } else { client.flushDataObject(destFile); } } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message + " in the specified directory."); } catch (IOException e) { Console.WriteLine(e.Message); } }