dmolony-DiskBrowser/src/com/bytezone/diskbrowser/disk/SectorListConverter.java

72 lines
2.2 KiB
Java
Raw Normal View History

2015-06-01 09:35:51 +00:00
package com.bytezone.diskbrowser.disk;
import java.util.ArrayList;
import java.util.List;
2020-02-08 08:13:28 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
public class SectorListConverter
2020-02-08 08:13:28 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
public final List<DiskAddress> sectors;
public final String sectorText;
2020-02-08 08:13:28 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
public SectorListConverter (String text, Disk disk)
2020-02-08 08:13:28 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
2020-02-02 10:17:49 +00:00
sectors = new ArrayList<> ();
2015-06-01 09:35:51 +00:00
sectorText = text;
2021-05-21 03:34:33 +00:00
for (String s : text.split (";"))
2015-06-01 09:35:51 +00:00
{
int pos = s.indexOf ('-');
if (pos > 0)
{
int lo = Integer.parseInt (s.substring (0, pos));
int hi = Integer.parseInt (s.substring (pos + 1));
for (int i = lo; i <= hi; i++)
sectors.add (disk.getDiskAddress (i));
}
else
sectors.add (disk.getDiskAddress (Integer.parseInt (s)));
}
}
2020-02-08 08:13:28 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
public SectorListConverter (List<DiskAddress> sectors)
2020-02-08 08:13:28 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
this.sectors = sectors;
StringBuilder text = new StringBuilder ();
int firstBlock = -2;
int runLength = 0;
for (DiskAddress da : sectors)
{
2020-04-10 23:47:52 +00:00
if (da.getBlockNo () == firstBlock + 1 + runLength)
2015-06-01 09:35:51 +00:00
{
++runLength;
continue;
}
if (firstBlock >= 0)
addToText (text, firstBlock, runLength);
2020-04-10 23:47:52 +00:00
firstBlock = da.getBlockNo ();
2015-06-01 09:35:51 +00:00
runLength = 0;
}
addToText (text, firstBlock, runLength);
sectorText = text.deleteCharAt (text.length () - 1).toString ();
}
2020-02-08 08:13:28 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
private void addToText (StringBuilder text, int firstBlock, int runLength)
2020-02-08 08:13:28 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
if (runLength == 0)
text.append (firstBlock + ";");
else
text.append (firstBlock + "-" + (firstBlock + runLength) + ";");
}
}