Code Examples JNQ

smb client software

Mount

				
					PasswordCredentials cr = new PasswordCredentials(   “userName”, “password”, “domain”);

// Create a new Mount Point

Mount mt = new Mount(“IpAddress”,“ShareName”, cr);

// Close all connections and mount points

Client.stop();
				
			

Open, read, write and delete file

				
					PasswordCredentials cr = new PasswordCredentials(   “userName”, “password”, “domain”);

// Create a new Mount Point

Mount mt = new Mount(“IpAddress”,“ShareName”, cr);

Params pr = new File.Params(File.ACCESS_WRITE, File.SHARE_FULL,

                                                          File.DISPOSITION_OPEN, false);

File file = new File(mt, “text.txt”, pr);

Buffer buff = new Buffer(DATASIZE);

// read DATASIZE from the begginig of the file.

file.read(buff);

// write the buff from the offset DATASIZE.

file.write(buff);

// mark the file for delete.

file.deleteOnClose();

// close the file.

file.close();

// close the mount point

mt.close();
				
			

Delete file by name

				
					PasswordCredentials cr = new PasswordCredentials(  “userName”, “password”, “domain”);

// Create a new Mount Point

Mount mt = new Mount(“IpAddress”,”ShareName”, cr);

File.delete(mt, “text.txt”);

// close the mount point

mt.close();
				
			

Browsing directory

				
					PasswordCredentials cr = new PasswordCredentials(  “userName”, “password”, “domain”);

// Create a new Mount Point

Mount mt = new Mount(“IpAddress”,“ShareName”, cr);

Directory dir = new Directory(mt, “dir1”);

Directory.Entry entry;

System.out.println(DIR + ” scan:”);

do {

     entry = dir.next();

     if (null != entry)

          System.out.println(entry.name + ” : size = “ + entry.info.eof);

} while (entry != null);

// close the directory browsing.

dir.close();

// close the mount point

mt.close();
				
			

Access via Streaming

				
					PasswordCredentials cr = new PasswordCredentials(  “userName”, “password”, “domain”);

// Create a new Mount Point

Mount mt = new Mount(“IpAddress”,“ShareName”, cr);

// create a new file.

File.Params fileParams = new File.Params(

                     File.ACCESS_READ | File.ACCESS_WRITE |

                     File.ACCESS_DELETE, File.SHARE_FULL,

                     File.DISPOSITION_OPEN_IF, false);

File file = new File(mt, “myCreatedFile.txt”, fileParams);

// create output stream

OutputStream outputStream = new SmbOutputStream(file);

// create some data to write

int totalAmountOfData = 1024 * 1024 * 10;

byte [] data = createByteBuffer( totalAmountOfData);

 

// write all the bytes — buffering is done by TCP networking code

outputStream.write(data);

 

// alternative way of writing all the bytes with a little more control

int amountToWriteEachTime = 1024 * 1024;

int currentPosition = 0;

while (currentPosition < data.length) {

     file.setPosition( currentPosition);

     outputStream.write(data, currentPosition, amountToWriteEachTime);

     currentPosition += amountToWriteEachTime;

}

// close the stream

outputStream.close()

// close the mount point

mt.close();
				
			

Enumerate the shares available

				
					Iterator iterator = Network.enumerateShares(smbServer, credentials);

while (iterator.hasNext()) {

Share.Info shareInfo = (Share.Info) iterator.next();

System.out.println(“share name = ” + shareInfo.name);

}
				
			

Enumerate the servers available

				
					iterator = Network.enumerateServers(domain, credentials);

while (iterator.hasNext()) {

String serverInfo = (String) iterator.next();

System.out.println(“server name = ” + serverInfo);

}