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
5 changes: 5 additions & 0 deletions homework-g596-proskurina/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@ private static void exists(String fileName) throws FileNotFoundException {
}
}

public FileWorker() {
}

public static String read(String fileName) throws FileNotFoundException {

StringBuffer inputData = new StringBuffer();
exists(fileName);
File file = new File(fileName);

try (BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()))) {
String s;
while ((s = in.readLine()) != null) {
inputData.append(s);
inputData.append("\n");
try {
BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()));
try {
String s;
while ((s = in.readLine()) != null) {
inputData.append(s);
inputData.append("\n");
}
} finally {
in.close();
}

} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -50,4 +57,4 @@ public static void write(String fileName, String text) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ru.mipt.java2016.homework.base.task2.KeyValueStorage;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -13,7 +12,10 @@
*/
public class ImplementationKeyValueStorage<K, V> implements KeyValueStorage<K, V> {

private final Map<K, V> map = new HashMap<>();
private final HashMap<K, V> map = new HashMap<>();

private final String keyName;
private final String valueName;

private final SerialiserInterface<K> keySerialiser;
private final SerialiserInterface<V> valueSerialiser;
Expand All @@ -28,11 +30,14 @@ public ImplementationKeyValueStorage(String keyName, String valueName,
SerialiserInterface<K> keySerialiser, SerialiserInterface<V> valueSerialiser,
String directoryPath) {

this.keyName = keyName;
this.valueName = valueName;

this.keySerialiser = keySerialiser;
this.valueSerialiser = valueSerialiser;

file = new FileWorker();
fileName = directoryPath + File.separator + "myFile.db";
fileName = directoryPath + "/myFile.db";

try {
String inputData = file.read(fileName);
Expand Down Expand Up @@ -62,60 +67,57 @@ public ImplementationKeyValueStorage(String keyName, String valueName,

}

private void checkIfFileIsOpen() {
if (!openFlag) {
throw new RuntimeException("Storage is closed");
}
}

@Override
public V read(K key) {
checkIfFileIsOpen();
return map.get(key);

if (openFlag) {
return map.get(key);
} else {
throw new RuntimeException("Storage is closed");
}
}

@Override
public boolean exists(K key) {
checkIfFileIsOpen();
return map.containsKey(key);
}

@Override
public void write(K key, V value) {
checkIfFileIsOpen();
map.put(key, value);
if (openFlag) {
map.put(key, value);
} else {
throw new RuntimeException("Storage is closed");
}
}

@Override
public void delete(K key) {
checkIfFileIsOpen();
map.remove(key);
}

@Override
public Iterator<K> readKeys() {
checkIfFileIsOpen();
return map.keySet().iterator();
if (openFlag) {
return map.keySet().iterator();
} else {
throw new RuntimeException("Storage is closed");
}
}

@Override
public int size() {
checkIfFileIsOpen();
return map.size();
}

@Override
public void close() {
checkIfFileIsOpen();
openFlag = false;
writeData();
map.clear();
}

public void writeData() {
StringBuffer text = new StringBuffer(VALIDATION_STRING + "\n");
text.append(keySerialiser.getType()).append('\n').append(valueSerialiser.getType()).append('\n');
text.append(keyName).append('\n').append(valueName).append('\n');
text.append(map.size()).append('\n');
for (Map.Entry<K, V> entry : map.entrySet()) {
text.append(keySerialiser.serialise(entry.getKey()))
Expand All @@ -127,4 +129,4 @@ public void writeData() {

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ public Double deserialise(String inString) {
return Double.parseDouble(inString);
}

@Override
public String getType() {
return "Double";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public Integer deserialise(String inString) {
return Integer.parseInt(inString);
}

@Override
public String getType() {
return "Integer";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ public String deserialise(String inString) {
return inString;
}

@Override
public String getType() {
return "String";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public interface SerialiserInterface<T> {

T deserialise(String inString);

String getType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package ru.mipt.java2016.homework.g596.proskurina.task3;

import java.io.*;
import java.nio.ByteBuffer;

/**
* Created by Lenovo on 31.10.2016.
*/
public class FileWorker implements Closeable {

private final File file;
private final String fileName;

private InputStream readBuffer;
private OutputStream writeBuffer;

private long currentPositionInStream;

public FileWorker(String fileName) {
this.file = new File(fileName);
this.fileName = fileName;
}

public void createFile() {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("file didn't created");
throw new RuntimeException(e);
}
}

public void rename(String newName) {
File newFile = new File(newName);
file.renameTo(newFile);
}

public void appendMode() {
try {
close();
writeBuffer = new FileOutputStream(file.getAbsoluteFile(), true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public boolean exist() {
return file.exists();
}

private boolean innerExist() throws FileNotFoundException {
if (!exist()) {
throw new FileNotFoundException(fileName);
}
return true;
}

public void flushSubmit() {
if (writeBuffer != null) {
try {
writeBuffer.flush();
writeBuffer.close();
writeBuffer = null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

public long write(String str) {
try {
innerExist();
if (writeBuffer == null) {
writeBuffer = new BufferedOutputStream(new FileOutputStream(file.getAbsoluteFile()));
}
byte[] bytes = str.getBytes();
byte[] bytesNumber = ByteBuffer.allocate(4).putInt(bytes.length).array();
writeBuffer.write(bytesNumber);
writeBuffer.write(bytes);
return 4 + bytes.length;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public String read() {
try {
innerExist();
if (readBuffer == null) {
readBuffer = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile()));
currentPositionInStream = 0;
}
if (readBuffer.available() < 4) {
readBuffer.close();
readBuffer = null;
return null;
}
byte[] bytesNumberArray = new byte[4];
int bytesNumberArraySize = readBuffer.read(bytesNumberArray, 0, 4);
//addToCalc(bytes);
if (bytesNumberArraySize < 4) {
readBuffer.close();
throw new RuntimeException("Error in reading bytes number");
}
currentPositionInStream += bytesNumberArraySize;
int bytesNumber = ByteBuffer.wrap(bytesNumberArray).getInt();
byte[] bytesArray = new byte[bytesNumber];
int bytesArraySize = readBuffer.read(bytesArray, 0, bytesNumber);
if (bytesArraySize < bytesNumber) {
readBuffer.close();
throw new RuntimeException("Error in reading bytes");
}
currentPositionInStream += bytesArraySize;
return new String(bytesArray);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public long fileLength() {
try (FileInputStream inputStream = new FileInputStream(file.getAbsoluteFile())) {
return inputStream.available();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void goToPosition(long position) {
try {
innerExist();
if (readBuffer == null || currentPositionInStream > position) {
if (readBuffer != null) {
readBuffer.close();
}
readBuffer = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile()));
currentPositionInStream = 0;
}
while (currentPositionInStream < position) {
currentPositionInStream += readBuffer.skip(position - currentPositionInStream);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void delete() {
file.delete();
}

@Override
public void close() {
try {
if (readBuffer != null) {
readBuffer.close();
currentPositionInStream = 0;
readBuffer = null;
}
if (writeBuffer != null) {
writeBuffer.flush();
writeBuffer.close();
writeBuffer = null;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading