Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions fs/src/fs/org/jnode/fs/fat/AbstractDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,15 @@ protected void initialize(long myCluster, long parentCluster) {
public abstract void flush() throws IOException;

/**
* Read the contents of this directory from the given byte array
* Read the contents of this directory from the given buffer
*
* @param src
*/
protected synchronized void read(byte[] src) {
protected synchronized void read(ByteBuffer src) {
int size = entries.size();
for (int i = 0; i < size; i++) {
int index = i * FatConstants.DIR_ENTRY_SIZE;
if (src[index] == 0) {
if (src.get(index) == 0) {
entries.set(i, null);
} else {
FatBasicDirEntry entry = FatDirEntry.fatDirEntryFactory(this, src, index);
Expand All @@ -399,20 +399,20 @@ protected synchronized void read(byte[] src) {
}

/**
* Write the contents of this directory to the given device at the given
* offset.
* Write the contents of this directory to the given buffer
*
* @param dest
*/
protected synchronized void write(byte[] dest) {
protected synchronized void write(ByteBuffer dest) {
int size = entries.size();
byte[] empty = new byte[FatConstants.DIR_ENTRY_SIZE];
for (int i = 0; i < size; i++) {
FatBasicDirEntry entry = entries.get(i);
if (entry != null) {
entry.write(dest, i * FatConstants.DIR_ENTRY_SIZE);
} else {
System.arraycopy(empty, 0, dest, i * FatConstants.DIR_ENTRY_SIZE, empty.length);
dest.position(i * FatConstants.DIR_ENTRY_SIZE);
dest.put(empty);
}
}
}
Expand Down
44 changes: 24 additions & 20 deletions fs/src/fs/org/jnode/fs/fat/BootSector.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,41 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.jnode.driver.block.BlockDeviceAPI;
import org.jnode.driver.block.Geometry;
import org.jnode.driver.block.GeometryException;
import org.jnode.partitions.ibm.IBMPartitionTable;
import org.jnode.partitions.ibm.IBMPartitionTableEntry;
import org.jnode.partitions.ibm.IBMPartitionTypes;
import org.jnode.util.LittleEndian;
import org.jnode.util.NumberUtils;

/**
* @author epr
*/
public class BootSector {

private byte[] data;
private ByteBuffer data;
private boolean dirty;
private final IBMPartitionTableEntry[] partitions;

public BootSector(int size) {
data = new byte[size];
data = ByteBuffer.allocate(size);
data.order(ByteOrder.LITTLE_ENDIAN);
dirty = false;
partitions = new IBMPartitionTableEntry[4];
}

public BootSector(byte[] src) {
data = new byte[src.length];
System.arraycopy(src, 0, data, 0, src.length);
data = ByteBuffer.wrap(src.clone());
data.order(ByteOrder.LITTLE_ENDIAN);
dirty = false;
partitions = new IBMPartitionTableEntry[4];
}

public boolean isaValidBootSector() {
return IBMPartitionTable.containsPartitionTable(data);
return IBMPartitionTable.containsPartitionTable(data.array());
}

/**
Expand All @@ -64,8 +65,8 @@ public boolean isaValidBootSector() {
* @param device
*/
public synchronized void read(BlockDeviceAPI device) throws IOException {
device.read(0, ByteBuffer.wrap(data));

data.clear();
device.read(0, data);
dirty = false;
}

Expand All @@ -75,7 +76,8 @@ public synchronized void read(BlockDeviceAPI device) throws IOException {
* @param device
*/
public synchronized void write(BlockDeviceAPI device) throws IOException {
device.write(0, ByteBuffer.wrap(data));
data.rewind();
device.write(0, data);
dirty = false;
}

Expand All @@ -87,7 +89,7 @@ public synchronized void write(BlockDeviceAPI device) throws IOException {
public String getOemName() {
StringBuilder b = new StringBuilder(8);
for (int i = 0; i < 8; i++) {
int v = data[0x3 + i];
int v = data.get(0x3 + i) & 0xFF;
b.append((char) v);
}
return b.toString();
Expand All @@ -104,8 +106,9 @@ public void setOemName(String name) {
} else {
ch = (char) 0;
}
set8(0x3 + i, ch);
data.put(0x3 + i, (byte) ch);
}
dirty = true;
}

/**
Expand Down Expand Up @@ -291,7 +294,7 @@ public void setNrHiddenSectors(int v) {
* @return int
*/
protected int get8(int offset) {
return LittleEndian.getUInt8(data, offset);
return data.get(offset) & 0xFF;
}

/**
Expand All @@ -300,7 +303,7 @@ protected int get8(int offset) {
* @param offset
*/
protected void set8(int offset, int value) {
LittleEndian.setInt8(data, offset, value);
data.put(offset, (byte) value);
dirty = true;
}

Expand All @@ -311,7 +314,7 @@ protected void set8(int offset, int value) {
* @return int
*/
protected int get16(int offset) {
return LittleEndian.getUInt16(data, offset);
return data.getShort(offset) & 0xFFFF;
}

/**
Expand All @@ -320,7 +323,7 @@ protected int get16(int offset) {
* @param offset
*/
protected void set16(int offset, int value) {
LittleEndian.setInt16(data, offset, value);
data.putShort(offset, (short) value);
dirty = true;
}

Expand All @@ -331,7 +334,7 @@ protected void set16(int offset, int value) {
* @return int
*/
protected long get32(int offset) {
return LittleEndian.getUInt32(data, offset);
return data.getInt(offset) & 0xFFFFFFFFL;
}

/**
Expand All @@ -340,7 +343,7 @@ protected long get32(int offset) {
* @param offset
*/
protected void set32(int offset, long value) {
LittleEndian.setInt32(data, offset, (int) value);
data.putInt(offset, (int) value);
dirty = true;
}

Expand Down Expand Up @@ -377,7 +380,7 @@ public IBMPartitionTableEntry initPartitions(Geometry geom, IBMPartitionTypes fi

public synchronized IBMPartitionTableEntry getPartition(int partNr) {
if (partitions[partNr] == null) {
partitions[partNr] = new IBMPartitionTableEntry(null, data, partNr);
partitions[partNr] = new IBMPartitionTableEntry(null, data.array(), partNr);
}
return partitions[partNr];
}
Expand Down Expand Up @@ -422,10 +425,11 @@ public String toString() {
res.append(getNrRootDirEntries());
res.append('\n');

for (int i = 0; i < data.length / 16; i++) {
byte[] arr = data.array();
for (int i = 0; i < arr.length / 16; i++) {
res.append(Integer.toHexString(i));
res.append('-');
res.append(NumberUtils.hex(data, i * 16, 16));
res.append(NumberUtils.hex(arr, i * 16, 16));
res.append('\n');
}

Expand Down
48 changes: 17 additions & 31 deletions fs/src/fs/org/jnode/fs/fat/Fat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import org.jnode.driver.block.BlockDeviceAPI;
import org.jnode.fs.FileSystemFullException;
Expand Down Expand Up @@ -79,35 +80,26 @@ public Fat(FatType bitSize, int mediumDescriptor, int nrSectors, int sectorSize)
* @param device
*/
public synchronized void read(BlockDeviceAPI device, long offset) throws IOException {
byte[] data = new byte[nrSectors * sectorSize];
device.read(offset, ByteBuffer.wrap(data));
ByteBuffer data = ByteBuffer.allocate(nrSectors * sectorSize);
data.order(ByteOrder.LITTLE_ENDIAN);
device.read(offset, data);
for (int i = 0; i < entries.length; i++) {
int idx, b1, b2, v;
int idx, v;
switch (fatType) {
case FAT12:
idx = (int) (i * 1.5);
b1 = data[idx] & 0xFF;
b2 = data[idx + 1] & 0xFF;
v = (b2 << 8) | b1;
v = data.getShort(idx) & 0xFFFF;
if ((i % 2) == 0) {
entries[i] = v & 0xFFF;
} else {
entries[i] = v >> 4;
}
break;
case FAT16:
idx = i * 2;
b1 = data[idx] & 0xFF;
b2 = data[idx + 1] & 0xFF;
entries[i] = (b2 << 8) | b1;
entries[i] = data.getShort(i * 2) & 0xFFFF;
break;
case FAT32:
idx = i * 4;
long l1 = data[idx] & 0xFF;
long l2 = data[idx + 1] & 0xFF;
long l3 = data[idx + 2] & 0xFF;
long l4 = data[idx + 3] & 0xFF;
entries[i] = (l4 << 24) | (l3 << 16) | (l2 << 8) | l1;
entries[i] = data.getInt(i * 4) & 0xFFFFFFFFL;
break;
}
}
Expand All @@ -120,37 +112,31 @@ public synchronized void read(BlockDeviceAPI device, long offset) throws IOExcep
* @param device
*/
public synchronized void write(BlockDeviceAPI device, long offset) throws IOException {
byte[] data = new byte[nrSectors * sectorSize];
ByteBuffer data = ByteBuffer.allocate(nrSectors * sectorSize);
data.order(ByteOrder.LITTLE_ENDIAN);
for (int i = 0; i < entries.length; i++) {
long v = entries[i];
int idx;
switch (fatType) {
case FAT12:
idx = (int) (i * 1.5);
if ((i % 2) == 0) {
data[idx] = (byte) (v & 0xFF);
data[idx + 1] = (byte) ((v >> 8) & 0x0F);
data.put(idx, (byte) (v & 0xFF));
data.put(idx + 1, (byte) ((v >> 8) & 0x0F));
} else {
data[idx] |= (byte) ((v & 0x0F) << 4);
data[idx + 1] = (byte) ((v >> 4) & 0xFF);
data.put(idx, (byte) (data.get(idx) | ((v & 0x0F) << 4)));
data.put(idx + 1, (byte) ((v >> 4) & 0xFF));
}
break;
case FAT16:
idx = i << 1;
data[idx] = (byte) (v & 0xFF);
data[idx + 1] = (byte) ((v >> 8) & 0xFF);
data.putShort(i << 1, (short) v);
break;
case FAT32:
idx = i << 2;
data[idx] = (byte) (v & 0xFF);
data[idx + 1] = (byte) ((v >> 8) & 0xFF);
data[idx + 2] = (byte) ((v >> 16) & 0xFF);
data[idx + 3] = (byte) ((v >> 24) & 0xFF);
data.putInt(i << 2, (int) v);
break;
}

}
device.write(offset, ByteBuffer.wrap(data));
device.write(offset, data);
this.dirty = false;
}

Expand Down
24 changes: 19 additions & 5 deletions fs/src/fs/org/jnode/fs/fat/FatBasicDirEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,37 @@

package org.jnode.fs.fat;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
* @author gbin
*/
public class FatBasicDirEntry extends FatObject implements FatConstants {

protected byte[] rawData = new byte[32];
protected ByteBuffer rawData = ByteBuffer.allocate(32);

{
rawData.order(ByteOrder.LITTLE_ENDIAN);
}

public FatBasicDirEntry(AbstractDirectory dir) {
super(dir.getFatFileSystem());
}

public FatBasicDirEntry(AbstractDirectory dir, byte[] src, int offset) {
public FatBasicDirEntry(AbstractDirectory dir, ByteBuffer src, int offset) {
super(dir.getFatFileSystem());
System.arraycopy(src, offset, rawData, 0, 32);
rawData.clear();
int oldLimit = src.limit();
src.position(offset);
src.limit(offset + 32);
rawData.put(src);
src.limit(oldLimit);
}

public void write(byte[] dest, int offset) {
System.arraycopy(rawData, 0, dest, offset, 32);
public void write(ByteBuffer dest, int offset) {
rawData.rewind();
dest.position(offset);
dest.put(rawData);
}
}
Loading
Loading