Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proskurina-596g-task 3 #320

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
@@ -1,50 +1,165 @@
package ru.mipt.java2016.homework.g596.proskurina.task2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше перенеси новое в .task3, в .task2 оставь всё, как оно было во втором задании


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

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

private static void exists(String fileName) throws FileNotFoundException {
File file = new File(fileName);
if (!file.exists()) {
throw new FileNotFoundException(file.getName());
private final File file;
private final String fileName;

private InputStream readBuffer = null;
private OutputStream writeBuffer = null;

private long currentPositionInStream = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В java по умолчанию поля инициализируются именно так


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 static String read(String fileName) throws FileNotFoundException {
public void rename(String newName) {
File newFile = new File(newName);
file.renameTo(newFile);
}

StringBuffer inputData = new StringBuffer();
exists(fileName);
File file = new File(fileName);
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;
}

try (BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()))) {
String s;
while ((s = in.readLine()) != null) {
inputData.append(s);
inputData.append("\n");
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);
}
return inputData.toString();
}

public static void write(String fileName, String text) {
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);
//addToCalc(bytes);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не бросай закомментированный код в репозитории. Ему так грустно и одиноко от этого

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);
}
}

File file = new File(fileName);
public void goToPosition(long position) {
try {
if (!file.exists()) {
file.createNewFile();
innerExist();
if (readBuffer == null || currentPositionInStream > position) {
if (readBuffer != null) {
readBuffer.close();
}
readBuffer = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile()));
currentPositionInStream = 0;
}
PrintWriter out = new PrintWriter(file.getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
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);
Expand Down
Loading