Мне нужно выполнить задачу, как показано ниже: -
1). Загрузка файла в основном месте: -
Я хочу прочитать файл и записать его в основное местоположение (удаленный файловый сервер).
2). Загрузка файла в нескольких дополнительных местах: -
В то же время, когда выполняется запись в основное местоположение, параллельно
Я хочу прочитать некоторые куски байтов из основного файла местоположения
и запись его в несколько вторичных местоположений.
Я попробовал приведенную ниже программу для вышеуказанного подхода: -
BufferedInputStream bin = null;
ReadableByteChannel channel = null;
int bufferSize = 1048576;
int readBufferSize = 1024*4;
java.nio.ByteBuffer byteBuffer = java.nio.ByteBuffer.allocate(readBufferSize);
InputStream is = new FileInputStream(new File("D:\\Harisingh\\300MB.txt"));
bin = new BufferedInputStream(is,bufferSize);
channel = Channels.newChannel(bin);
int retryCnt = 0;
ByteArrayOutputStream baOS = new ByteArrayOutputStream(bufferSize);
int totalBytes=0;
int itrCount=0;
int maxIterateCnt = 1;
int len;
//primary location writing
SmbFile smbFile = new SmbFile("smb://user:Password@fileserver1ind1.hqdev.india/data/Harisingh/collab_4_1_4/primary.txt");
BufferedOutputStream bFout = new BufferedOutputStream(new SmbFileOutputStream(smbFile));
SmbFileInputStream fis = new SmbFileInputStream("smb://user:Password@fileserver1ind1.hqdev.india/data/Harisingh/collab_4_1_4/primary.txt");
BufferedInputStream binPrimary = new BufferedInputStream(fis);
SmbFileOutputStream secLocation1= new SmbFileOutputStream(new SmbFile("smb://user:Password@fileserver1ind1.hqdev.india/data/Harisingh/collab_4_1_4/Secondary1.txt"));
SmbFileOutputStream secLocation2 = new SmbFileOutputStream(new SmbFile("smb://user:Password@fileserver1ind1.hqdev.india/data/Harisingh/collab_4_1_4/Secondary2.txt"));
SmbFileOutputStream secLocation3 = new SmbFileOutputStream(new SmbFile("smb://user:Password@fileserver1ind1.hqdev.india/data/Harisingh/Secondary/Secondary3.txt"));
try {
if(bufferSize > readBufferSize){
maxIterateCnt = bufferSize/readBufferSize;
}
while((len=channel.read(byteBuffer))>=0)
{
itrCount++;
totalBytes+=len;
baOS.write(byteBuffer.array(),0,len);
if(itrCount>=maxIterateCnt)
{
//primary location writing
try{
bFout.write(baOS.toByteArray(),0,totalBytes);
}catch(Exception se)
{
}
// secondary location writing
new Thread(){
public void run(){
System.out.println("Thread Running");
try {
int count;
byte[] readByteArray = new byte[1024*4];
while ((count = binPrimary.read(readByteArray)) != -1)
{
secLocation1.write(readByteArray, 0, count);
secLocation2.write(readByteArray, 0, count);
secLocation3.write(readByteArray, 0, count);
readByteArray = new byte[1024*4];
count= 0;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
totalBytes=0;
baOS.reset();
itrCount=0;
}
byteBuffer.clear();
}
//primary location writing
try{
bFout.write(baOS.toByteArray(),0,totalBytes);
}catch(Exception se)
{
}
bFout.flush();
bFout.close();
int count;
// secondary location writing
new Thread(){
public void run(){
System.out.println("Thread Running");
try {
int count;
byte[] readByteArray = new byte[1024*4];
while ((count = binPrimary.read(readByteArray)) != -1)
{
secLocation1.write(readByteArray, 0, count);
secLocation2.write(readByteArray, 0, count);
secLocation3.write(readByteArray, 0, count);
readByteArray = new byte[1024*4];
count= 0;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
Теперь с помощью вышеприведенной программы она записывает файл в первичное местоположение по основному потоку, а запись вторичного расположения выполняется в отдельном потоке, но я сталкиваюсь с проблемой отсутствия записи некоторых байтов в некоторых вторичных местоположениях из-за многопоточности.
FYI
Этот вопрос относится только к io stream. Он не является специфичным для JCIFS, поэтому вы можете использовать ту же программу с простым потоком ввода-вывода, не требуя smb-ввода.
Не могли бы вы помочь мне разобраться с этим?