-
Notifications
You must be signed in to change notification settings - Fork 80
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
ksushkapr
wants to merge
3
commits into
fediq:master
Choose a base branch
from
ksushkapr:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 139 additions & 24 deletions
163
...-proskurina/src/main/java/ru/mipt/java2016/homework/g596/proskurina/task2/FileWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,165 @@ | ||
package ru.mipt.java2016.homework.g596.proskurina.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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше перенеси новое в .task3, в .task2 оставь всё, как оно было во втором задании