jcifs.smb.SmbRandomAccessFile Java Examples

The following examples show how to use jcifs.smb.SmbRandomAccessFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RandomAccessFileTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testWriteVerify () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            int bufSize = 4096;
            int l = 1024;
            int off = 2048;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.seek(off);
                writeRandom(bufSize, l, raf);
                assertEquals(l + off, raf.getFilePointer());
                raf.setLength(raf.getFilePointer() + l);
            }

            try ( InputStream is = f.getInputStream() ) {
                verifyZero(off, is);
                ReadWriteTest.verifyRandom(bufSize, l, false, is);
                verifyZero(l, is);
            }
        }
        finally {
            f.delete();
        }
    }
}
 
Example #2
Source File: RandomAccessFileTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testReadWriteSeeked () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            int bufSize = 4096;
            long l = 1024;
            int off = 2048;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.seek(off);
                writeRandom(bufSize, l, raf);
                assertEquals(l + off, raf.getFilePointer());
                raf.seek(off);
                assertEquals(off, raf.getFilePointer());
                verifyRandom(bufSize, l, raf);
                assertEquals(l + off, raf.getFilePointer());
            }
            assertEquals(l + off, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #3
Source File: RandomAccessFileTest.java    From jcifs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testReadOnly () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            try ( SmbFileOutputStream os = f.openOutputStream() ) {
                os.write(new byte[] {
                    0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
                });
            }

            byte[] buf = new byte[4];
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(4);
                raf.readFully(buf);
            }

            Assert.assertArrayEquals(new byte[] {
                0x4, 0x5, 0x6, 0x7
            }, buf);
        }
        finally {
            f.delete();
        }
    }
}
 
Example #4
Source File: RandomAccessFileTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testReadWrite () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            int bufSize = 4096;
            long l = 1024;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                writeRandom(bufSize, l, raf);
                assertEquals(l, raf.getFilePointer());
                raf.seek(0);
                assertEquals(0, raf.getFilePointer());
                verifyRandom(bufSize, l, raf);
                assertEquals(l, raf.getFilePointer());
            }
            assertEquals(l, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #5
Source File: RandomAccessFileTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testSetLengthTruncate () throws CIFSException, MalformedURLException, UnknownHostException {
    try ( SmbFile f = createTestFile() ) {
        try {
            long newLength = 1024L;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.seek(4096);
                raf.write(0);
                raf.setLength(newLength);
            }
            assertEquals(newLength, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #6
Source File: RandomAccessFileTest.java    From jcifs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testSetLengthTruncate () throws CIFSException, MalformedURLException, UnknownHostException {
    try ( SmbFile f = createTestFile() ) {
        try {
            long newLength = 1024L;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.seek(4096);
                raf.write(0);
                raf.setLength(newLength);
            }
            assertEquals(newLength, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #7
Source File: RandomAccessFileTest.java    From jcifs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testReadWrite () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            int bufSize = 4096;
            long l = 1024;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                writeRandom(bufSize, l, raf);
                assertEquals(l, raf.getFilePointer());
                raf.seek(0);
                assertEquals(0, raf.getFilePointer());
                verifyRandom(bufSize, l, raf);
                assertEquals(l, raf.getFilePointer());
            }
            assertEquals(l, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #8
Source File: RandomAccessFileTest.java    From jcifs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testReadWriteSeeked () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            int bufSize = 4096;
            long l = 1024;
            int off = 2048;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.seek(off);
                writeRandom(bufSize, l, raf);
                assertEquals(l + off, raf.getFilePointer());
                raf.seek(off);
                assertEquals(off, raf.getFilePointer());
                verifyRandom(bufSize, l, raf);
                assertEquals(l + off, raf.getFilePointer());
            }
            assertEquals(l + off, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #9
Source File: RandomAccessFileTest.java    From jcifs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testWriteVerify () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            int bufSize = 4096;
            int l = 1024;
            int off = 2048;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.seek(off);
                writeRandom(bufSize, l, raf);
                assertEquals(l + off, raf.getFilePointer());
                raf.setLength(raf.getFilePointer() + l);
            }

            try ( InputStream is = f.getInputStream() ) {
                verifyZero(off, is);
                ReadWriteTest.verifyRandom(bufSize, l, false, is);
                verifyZero(l, is);
            }
        }
        finally {
            f.delete();
        }
    }
}
 
Example #10
Source File: RandomAccessFileTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testReadOnly () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            try ( SmbFileOutputStream os = f.openOutputStream() ) {
                os.write(new byte[] {
                    0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
                });
            }

            byte[] buf = new byte[4];
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(4);
                raf.readFully(buf);
            }

            Assert.assertArrayEquals(new byte[] {
                0x4, 0x5, 0x6, 0x7
            }, buf);
        }
        finally {
            f.delete();
        }
    }
}
 
Example #11
Source File: FileOperationsTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testCopyFileLarge () throws IOException {
    long length = 4096 * 16 * 1024;
    try ( SmbFile f = createTestFile();
          SmbTreeHandle treeHandle = f.getTreeHandle() ) {
        // this is tremendously slow on SMB1
        try {
            Assume.assumeTrue("Not SMB2", treeHandle.isSMB2());

            try ( SmbFile d1 = createTestDirectory();
                  SmbFile t = new SmbFile(d1, makeRandomName()) ) {
                try {
                    try ( SmbRandomAccessFile ra = f.openRandomAccess("rw") ) {
                        ra.setLength(length);
                    }

                    f.copyTo(t);
                    assertTrue(f.exists());
                    assertEquals(f.length(), t.length());
                    assertEquals(f.getAttributes(), t.getAttributes());
                }
                finally {
                    d1.delete();
                }
            }
        }
        finally {
            f.delete();
        }
    }
}
 
Example #12
Source File: RandomAccessFileTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testSetLength () throws CIFSException, MalformedURLException, UnknownHostException {
    try ( SmbFile f = createTestFile() ) {
        try {
            long newLength = 4096L;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.setLength(newLength);
            }
            assertEquals(newLength, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #13
Source File: RandomAccessFileTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testReadOnlySeekOOB () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            try ( SmbFileOutputStream os = f.openOutputStream() ) {
                os.write(new byte[] {
                    0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
                });
            }

            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(10);
                Assert.assertEquals(-1, raf.read());
            }

            byte[] buf = new byte[4];
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(6);
                Assert.assertEquals(2, raf.read(buf));
                Assert.assertArrayEquals(new byte[] {
                    0x6, 0x7, 0x0, 0x0
                }, buf);
            }

            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(6);
                try {
                    raf.readFully(buf);
                    Assert.fail("Should have thrown exception");
                }
                catch ( SmbEndOfFileException e ) {}
            }

        }
        finally {
            f.delete();
        }
    }
}
 
Example #14
Source File: ReadWriteTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testRandomAccess () throws CIFSException, MalformedURLException, UnknownHostException {
    try ( SmbFile f = createTestFile() ) {
        try {
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {

            }
        }
        finally {
            f.delete();
        }
    }
}
 
Example #15
Source File: FileOperationsTest.java    From jcifs-ng with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testCopyFileLargeNoAlign () throws IOException {
    long length = 4096 * 16 * 1024 + 13;
    try ( SmbFile f = createTestFile();
          SmbTreeHandle treeHandle = f.getTreeHandle() ) {
        // this is tremendously slow on SMB1
        try {
            Assume.assumeTrue("Not SMB2", treeHandle.isSMB2());
            try ( SmbFile d1 = createTestDirectory();
                  SmbFile t = new SmbFile(d1, makeRandomName()) ) {
                try {
                    try ( SmbRandomAccessFile ra = f.openRandomAccess("rw") ) {
                        ra.setLength(length);
                    }

                    f.copyTo(t);
                    assertTrue(f.exists());
                    assertEquals(f.length(), t.length());
                    assertEquals(f.getAttributes(), t.getAttributes());
                }
                finally {
                    d1.delete();
                }
            }
        }
        finally {
            f.delete();
        }
    }
}
 
Example #16
Source File: FileOperationsTest.java    From jcifs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testCopyFileLarge () throws IOException {
    long length = 4096 * 16 * 1024;
    try ( SmbFile f = createTestFile();
          SmbTreeHandle treeHandle = f.getTreeHandle() ) {
        // this is tremendously slow on SMB1
        try {
            Assume.assumeTrue("Not SMB2", treeHandle.isSMB2());

            try ( SmbFile d1 = createTestDirectory();
                  SmbFile t = new SmbFile(d1, makeRandomName()) ) {
                try {
                    try ( SmbRandomAccessFile ra = f.openRandomAccess("rw") ) {
                        ra.setLength(length);
                    }

                    f.copyTo(t);
                    assertTrue(f.exists());
                    assertEquals(f.length(), t.length());
                    assertEquals(f.getAttributes(), t.getAttributes());
                }
                finally {
                    d1.delete();
                }
            }
        }
        finally {
            f.delete();
        }
    }
}
 
Example #17
Source File: RandomAccessFileTest.java    From jcifs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testSetLength () throws CIFSException, MalformedURLException, UnknownHostException {
    try ( SmbFile f = createTestFile() ) {
        try {
            long newLength = 4096L;
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {
                raf.setLength(newLength);
            }
            assertEquals(newLength, f.length());
        }
        finally {
            f.delete();
        }
    }
}
 
Example #18
Source File: RandomAccessFileTest.java    From jcifs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testReadOnlySeekOOB () throws IOException {
    try ( SmbFile f = createTestFile() ) {
        try {
            try ( SmbFileOutputStream os = f.openOutputStream() ) {
                os.write(new byte[] {
                    0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
                });
            }

            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(10);
                Assert.assertEquals(-1, raf.read());
            }

            byte[] buf = new byte[4];
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(6);
                Assert.assertEquals(2, raf.read(buf));
                Assert.assertArrayEquals(new byte[] {
                    0x6, 0x7, 0x0, 0x0
                }, buf);
            }

            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "r") ) {
                raf.seek(6);
                try {
                    raf.readFully(buf);
                    Assert.fail("Should have thrown exception");
                }
                catch ( SmbEndOfFileException e ) {}
            }

        }
        finally {
            f.delete();
        }
    }
}
 
Example #19
Source File: ReadWriteTest.java    From jcifs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testRandomAccess () throws CIFSException, MalformedURLException, UnknownHostException {
    try ( SmbFile f = createTestFile() ) {
        try {
            try ( SmbRandomAccessFile raf = new SmbRandomAccessFile(f, "rw") ) {

            }
        }
        finally {
            f.delete();
        }
    }
}
 
Example #20
Source File: FileOperationsTest.java    From jcifs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testCopyFileLargeNoAlign () throws IOException {
    long length = 4096 * 16 * 1024 + 13;
    try ( SmbFile f = createTestFile();
          SmbTreeHandle treeHandle = f.getTreeHandle() ) {
        // this is tremendously slow on SMB1
        try {
            Assume.assumeTrue("Not SMB2", treeHandle.isSMB2());
            try ( SmbFile d1 = createTestDirectory();
                  SmbFile t = new SmbFile(d1, makeRandomName()) ) {
                try {
                    try ( SmbRandomAccessFile ra = f.openRandomAccess("rw") ) {
                        ra.setLength(length);
                    }

                    f.copyTo(t);
                    assertTrue(f.exists());
                    assertEquals(f.length(), t.length());
                    assertEquals(f.getAttributes(), t.getAttributes());
                }
                finally {
                    d1.delete();
                }
            }
        }
        finally {
            f.delete();
        }
    }
}