hierynomus / smbj

Server Message Block (SMB2, SMB3) implementation in Java
Other
713 stars 180 forks source link

Cannot read data in a file #699

Closed tvessiere closed 2 years ago

tvessiere commented 2 years ago

Hello there.

I'm a bit confuse... I'm not able to read the data in a file, the InputStream returned as no bytes inside.

Here is the code : ` try (DiskShare share = (DiskShare) session.connectShare("MyShare")) {

                File file = share.openFile("testPGMVT.txt",
                        EnumSet.of(AccessMask.GENERIC_READ),
                        null,
                        SMB2ShareAccess.ALL,
                        SMB2CreateDisposition.FILE_OPEN,
                        null);

                InputStream is =  file.getInputStream();
                System.out.println("bytes available : " + is.available());
                byte [] data = new byte[is.available()];
                is.read(data);
                String S = new String(data);
                System.out.println("Data in file : " + S);

                file.close();
                is.close();

            } 
            catch (IOException e) 
            {
            e.printStackTrace();
        }
        } 

The output : Bytes available : 0 Data in file : `

Can you help me ?

Thanks you.

isakulaksiz commented 2 years ago
 Session session = startConnection(context);
        DiskShare share = (DiskShare) session.connectShare("YOUR_SHARED_FOLDER_NAME");

        ArrayList<DATA_CLASS_OUT_FILE> fileContentArrayList = new ArrayList<>();
        com.hierynomus.smbj.share.File f = share.openFile("YOUR_HS3CASHOUTPATH", of(FILE_READ_DATA), null, SMB2ShareAccess.ALL, FILE_OPEN, null);
        InputStream in = f.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;

        while ((line = reader.readLine()) != null) {
          print(line)
        }

        in.close();
        f.close();

You can check it

tvessiere commented 2 years ago

Hello, what is H3CashOut class ?

isakulaksiz commented 2 years ago

Hi @tvessiere , You must have a data class where you define the values you hold in the HS3CASH.OUT file as variables

var reservationNumber: String,
 var objectNumber: String,
var guestName: String,
etc...
isakulaksiz commented 2 years ago

what is H3CashOut

You must have a data class where you define the values you hold in the HS3CASH.OUT file as variables

hierynomus commented 2 years ago

The FileInputStream will always return 0, as per the contract of java.io.InputStream, which is:

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

isakulaksiz commented 2 years ago

The FileInputStream will always return 0, as per the contract of java.io.InputStream, which is:

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

Can you give some advice on what I can use for this?

hierynomus commented 2 years ago
File file = share.openFile("testPGMVT.txt",
                        EnumSet.of(AccessMask.GENERIC_READ),
                        null,
                        SMB2ShareAccess.ALL,
                        SMB2CreateDisposition.FILE_OPEN,
                        null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
file.read(baos);
byte[] b = baos.toByteArray();
isakulaksiz commented 2 years ago

File file = share.openFile("testPGMVT.txt", EnumSet.of(AccessMask.GENERIC_READ), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); file.read(baos); byte[] b = baos.toByteArray();

thanks :)

tvessiere commented 2 years ago

File file = share.openFile("testPGMVT.txt", EnumSet.of(AccessMask.GENERIC_READ), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); file.read(baos); byte[] b = baos.toByteArray();

thanks :)

Thansk you man, i was busy at work, can't check it now, but i will run it quickly in the next days :)

have a nice week all of you.