代码拉取完成,页面将自动刷新
# from upstream pr: https://github.com/apache/commons-vfs/pull/93/
diff -Naur commons-vfs-2.1/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileRandomAccessContent.java commons-vfs-2.1-update/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileRandomAccessContent.java
--- commons-vfs-2.1/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileRandomAccessContent.java 2016-05-02 05:25:00.000000000 +0800
+++ commons-vfs-2.1-update/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileRandomAccessContent.java 2024-11-14 16:41:17.325043412 +0800
@@ -36,6 +36,8 @@
private final RandomAccessFile raf;
private final InputStream rafis;
+ private final static int BYTE_VALUE_MASK = 0xFF;
+
LocalFileRandomAccessContent(final File localFile, final RandomAccessMode mode) throws FileSystemException
{
super(mode);
@@ -50,7 +52,7 @@
{
try
{
- return raf.readByte();
+ return raf.readByte() & BYTE_VALUE_MASK;
}
catch (final EOFException e)
{
diff -Naur commons-vfs-2.1/core/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileRandomAccessContent.java commons-vfs-2.1-update/core/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileRandomAccessContent.java
--- commons-vfs-2.1/core/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileRandomAccessContent.java 2016-05-02 05:25:00.000000000 +0800
+++ commons-vfs-2.1-update/core/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileRandomAccessContent.java 2024-11-14 16:48:42.193267918 +0800
@@ -68,6 +68,8 @@
private final InputStream rafis;
+ private final static int BYTE_VALUE_MASK = 0xFF;
+
/**
* @param file The file to access.
* @param mode The access mode.
@@ -85,7 +87,7 @@
{
try
{
- return readByte();
+ return readByte() & BYTE_VALUE_MASK;
}
catch (final EOFException e)
{
@@ -248,7 +250,7 @@
{
if (filePointer < buf.length)
{
- return buf[filePointer++] & 0xFF;
+ return buf[filePointer++] & BYTE_VALUE_MASK;
}
else
{
@@ -429,12 +431,9 @@
*/
public static long toLong(final byte[] b)
{
- return ((((long) b[7]) & 0xFF) + ((((long) b[6]) & 0xFF) << 8)
- + ((((long) b[5]) & 0xFF) << 16)
- + ((((long) b[4]) & 0xFF) << 24)
- + ((((long) b[3]) & 0xFF) << 32)
- + ((((long) b[2]) & 0xFF) << 40)
- + ((((long) b[1]) & 0xFF) << 48) + ((((long) b[0]) & 0xFF) << 56));
+ return ((((long) b[7]) & BYTE_VALUE_MASK) + ((((long) b[6]) & BYTE_VALUE_MASK) << 8) + ((((long) b[5]) & BYTE_VALUE_MASK) << 16)
+ + ((((long) b[4]) & BYTE_VALUE_MASK) << 24) + ((((long) b[3]) & BYTE_VALUE_MASK) << 32) + ((((long) b[2]) & BYTE_VALUE_MASK) << 40)
+ + ((((long) b[1]) & BYTE_VALUE_MASK) << 48) + ((((long) b[0]) & BYTE_VALUE_MASK) << 56));
}
/**
@@ -483,7 +482,7 @@
*/
public static int toUnsignedShort(final byte[] b)
{
- return ((b[1] & 0xFF) + ((b[0] & 0xFF) << 8));
+ return ((b[1] & BYTE_VALUE_MASK) + ((b[0] & BYTE_VALUE_MASK) << 8));
}
/*
@@ -528,8 +527,8 @@
@Override
public void writeChar(final int v) throws IOException
{
- buffer2[0] = (byte) ((v >>> 8) & 0xFF);
- buffer2[1] = (byte) ((v >>> 0) & 0xFF);
+ buffer2[0] = (byte) ((v >>> 8) & BYTE_VALUE_MASK);
+ buffer2[1] = (byte) ((v >>> 0) & BYTE_VALUE_MASK);
write(buffer2);
}
@@ -578,10 +577,10 @@
@Override
public void writeInt(final int v) throws IOException
{
- buffer4[0] = (byte) ((v >>> 24) & 0xFF);
- buffer4[1] = (byte) ((v >>> 16) & 0xFF);
- buffer4[2] = (byte) ((v >>> 8) & 0xFF);
- buffer4[3] = (byte) (v & 0xFF);
+ buffer4[0] = (byte) ((v >>> 24) & BYTE_VALUE_MASK);
+ buffer4[1] = (byte) ((v >>> 16) & BYTE_VALUE_MASK);
+ buffer4[2] = (byte) ((v >>> 8) & BYTE_VALUE_MASK);
+ buffer4[3] = (byte) (v & BYTE_VALUE_MASK);
write(buffer4);
}
@@ -604,8 +603,8 @@
@Override
public void writeShort(final int v) throws IOException
{
- buffer2[0] = (byte) ((v >>> 8) & 0xFF);
- buffer2[1] = (byte) (v & 0xFF);
+ buffer2[0] = (byte) ((v >>> 8) & BYTE_VALUE_MASK);
+ buffer2[1] = (byte) (v & BYTE_VALUE_MASK);
write(buffer2);
}
diff -Naur commons-vfs-2.1/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalFileRandomAccessContentTestCase.java commons-vfs-2.1-update/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalFileRandomAccessContentTestCase.java
--- commons-vfs-2.1/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalFileRandomAccessContentTestCase.java 1970-01-01 08:00:00.000000000 +0800
+++ commons-vfs-2.1-update/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalFileRandomAccessContentTestCase.java 2024-11-14 17:13:42.036537949 +0800
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs2.provider.local;
+
+import org.apache.commons.vfs2.util.RandomAccessMode;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @since 2.7.0
+ */
+public class LocalFileRandomAccessContentTestCase {
+
+ private final int EOF = -1;
+
+ /**
+ * test LocalFileRandomAccessContent InputStream read one byte 0xff; see VFS-624
+ **/
+ @Test
+ public void testInputStreamRead0xff() throws IOException {
+ // open test file,this file has only one byte data 0xff
+ File file = new File("src/test/resources/test-data/0xff_file.txt");
+
+ // read test data,first data should be 0xFF instead of -1. Will read -1 finally (EOF)
+ try (InputStream in = new LocalFileRandomAccessContent(file, RandomAccessMode.READ).getInputStream()) {
+ // read first data
+ int read = in.read();
+ Assert.assertNotEquals(EOF, read);
+ Assert.assertEquals(0xFF, read);
+
+ // read EOF
+ Assert.assertEquals(EOF, in.read());
+ }
+ }
+}
diff -Naur commons-vfs-2.1/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/RamFileRandomAccessContentTestCase.java commons-vfs-2.1-update/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/RamFileRandomAccessContentTestCase.java
--- commons-vfs-2.1/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/RamFileRandomAccessContentTestCase.java 1970-01-01 08:00:00.000000000 +0800
+++ commons-vfs-2.1-update/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/RamFileRandomAccessContentTestCase.java 2024-11-14 17:13:51.048665754 +0800
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs2.provider.ram.test;
+
+import org.apache.commons.vfs2.FileObject;
+import org.apache.commons.vfs2.VFS;
+import org.apache.commons.vfs2.provider.ram.RamFileObject;
+import org.apache.commons.vfs2.provider.ram.RamFileRandomAccessContent;
+import org.apache.commons.vfs2.util.RandomAccessMode;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * @since 2.7.0
+ */
+public class RamFileRandomAccessContentTestCase {
+
+ private final int EOF = -1;
+
+ @Test
+ public void testInputStreamRead0xff() throws IOException {
+
+ // create ram file to test
+ final FileObject file = VFS.getManager().resolveFile("ram://file");
+ file.createFile();
+
+ // write test data,a single byte 0xFF
+ try (OutputStream out = file.getContent().getOutputStream()) {
+ out.write(0xFF);
+ }
+
+ // read test data,first data should be 0xFF instead of -1. Will read -1 finally (EOF)
+ try (InputStream in = new RamFileRandomAccessContent((RamFileObject) file, RandomAccessMode.READ).getInputStream()) {
+ // read first data
+ int read = in.read();
+ Assert.assertNotEquals(EOF, read);
+ Assert.assertEquals(0xFF, read);
+
+ // read EOF
+ Assert.assertEquals(EOF, in.read());
+ }
+ }
+}
diff -Naur commons-vfs-2.1/core/src/test/resources/test-data/0xff_file.txt commons-vfs-2.1-update/core/src/test/resources/test-data/0xff_file.txt
--- commons-vfs-2.1/core/src/test/resources/test-data/0xff_file.txt 1970-01-01 08:00:00.000000000 +0800
+++ commons-vfs-2.1-update/core/src/test/resources/test-data/0xff_file.txt 2024-11-14 16:53:52.922674525 +0800
@@ -0,0 +1 @@
+
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。